# make a dataframe
df = pd.DataFrame({
'a': [1,2,3,4]
})
# chained assignment does not work for columns created by .assign() in the same chain.
# KeyError: 'b'
df\
.assign(b=2*df['a'])\
.assign(c=2*df['b'])
I think chained assignment should work with each part of the chain returning the modified DataFrame with the new column, but gives a key error when referencing columns from previous assignments in the chain.
I understand that after modifying the original DataFrame, the copy does not have the name df, so it can't make the reference to df['b']. But, I think there should be a way to do chained assignment.
Running the following we see that using .assign() once does create a modified version of the DataFrame with 'b' as an accessible column.
# assigning one new column works fine. Column 'b' is accessible in the DataFrame.
df\
.assign(b=2*df['a'])
Also, I don't see any need to optimize assignment, because if I know that none of the columns I want to create depend on any of the other columns I want to create, I can write the following.
# there shouldn't be any need to optimize chained assignment, because if I know I need to create multiple new columns that don't refer to newly assigned columns, I can just do this.
df.assign(
b = 2 * df['a'],
c = 4 * df['a']
)
a b c
0 1 2 4
1 2 4 8
2 3 6 12
3 4 8 16
pd.show_versions()commit: None
python: 3.6.7.final.0
python-bits: 64
OS: Darwin
OS-release: 18.2.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
pandas: 0.23.4
pytest: 3.6.2
pip: 18.1
setuptools: 39.2.0
Cython: 0.28.3
numpy: 1.14.5
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: 1.7.5
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.5
feather: None
matplotlib: 2.2.2
openpyxl: 2.5.4
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.5
lxml: 4.2.2
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.8
pymysql: 0.8.1
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
I figured it out.
df\
.assign(b=2*df['a'])\
.assign(c=lambda df_copy: 2*df_copy['b'])
Sorry.
Most helpful comment
I figured it out.
Sorry.