是否可以将方法链中间的数据帧复制到新变量? 比如:
import pandas as pd
df = pd.DataFrame([[2,4,6],
[8,10,12],
[14,16,18],
])
df = (df.assign(new_var=100)
.div(2)
.copy_to_new_variable(df_imag) # Imaginated method to copy df to df_new.
.div(10)
)
print(df_imag)
然后会返回:
| | 0 | 1 | 2 | new_var |
| - | --- | --- | --- | ------- |
| 0 | 1.0 | 2.0 | 3.0 | 50.0 |
| 1 | 4.0 | 5.0 | 6.0 | 50.0 |
| 2 | 7.0 | 8.0 | 9.0 | 50.0 |
使用
:=
运算符:
df = (df_imag := df.assign(new_var=100).div(2)).div(10)
print(df)
print(df_imag)
打印:
0 1 2 new_var
0 0.1 0.2 0.3 5.0
1 0.4 0.5 0.6 5.0
2 0.7 0.8 0.9 5.0
0 1 2 new_var
0 1.0 2.0 3.0 50.0
1 4.0 5.0 6.0 50.0
2 7.0 8.0 9.0 50.0