In the following code, I created a function to return a DataFrame with a new column new_col with one-period shifted values of ori_col. But, a new column of the output has the name new_col rather than B. What am I missing here?
import pandas as pd
def add_col_diff(dframe, ori_col, new_col):
shift_1 = dframe[ori_col].shift(1)
return dframe.assign(new_col=shift_1)
data = {'A': [10, 11, 12]}
df = pd.DataFrame.from_dict(data)
df = add_col_diff(df, 'A', 'B')
df
This gives
A new_col
0 10 NaN
1 11 10.0
2 12 11.0
return dframe.assign('B'=shift_1)is not valid, so it presumably takes the argument name rather than its value?return dframe.assign(B=shift_1)will work, but I don't see how you reasonably convey that