Description
Problem description
I really like the assign function and it's ability to be applied in pipelines.
However, if you pass a dictionary via prefixed by **, the dictionary must only contain columns that already exist in the preceeding dataframe. So in a dataframe, that contains column 'A', and I want to construct column B as f(A) and column C = g(A, B), im forced to do
# Your code here
pd.DataFrame({'A':[1, 2]}).assign(**{'B': lambda x:f(x['A'])}).assign(**{'C': lambda x:g(x['A'], x['B])})
A B C
0 1 f(1) g(1, f(1))
1 2 f(2) g(2, f(2))
for some f and g and obtain a result like seen above. In extreme cases, this can lead to a lot of chained assign statements.
For convenience we could change the signature slightly to accept also *args, but every element in args should be such that the original assign function could be applied. In particular args could be a list of dictionaries.
With this, we could write the previous code as
# Your code here
pd.DataFrame({'A':[1, 2]}).assign(*[{'B': lambda x:f(x['A']),{'C': lambda x:g(x['A'], x['B])}])
Of course (also as it is right now) the user is responsible to construct a correct "computational graph" here. Additionally, the implementation I currently think of would use len(args) (-1 intermediate) copies of the original dataframe. However, using the stacked procedure above, this also happens.
Thus, we obtain a simpler syntactic way of using assign and we don't break the original implementation.
Output of pd.show_versions()
pandas: 0.20.3
pytest: 3.2.1
pip: None
setuptools: 36.5.0.post20170921
Cython: 0.26.1
numpy: 1.13.3
scipy: 0.19.1
xarray: None
IPython: 6.1.0
sphinx: 1.6.3
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.2
feather: None
matplotlib: 2.1.0
openpyxl: 2.4.8
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.0
bs4: 4.6.0
html5lib: 0.999999999
sqlalchemy: 1.1.13
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None