Create multiple columns from multiple return value of lambda function in python DataFrame

后端 未结 3 1512
萌比男神i
萌比男神i 2021-01-22 17:54

Att, I want to create multiple columns from lambda function\'s multiple return values in python DataFrame.

Similar with the last line of my demo code.

Is there

3条回答
  •  庸人自扰
    2021-01-22 18:21

    One way would be to wrap the return value in pd.Series in order to assign to new dataframe columns.

    g = lambda x: pd.Series(f_polyfit(x.y1, x.y2, x.y3, x.y5, x.y5, degree=1))
    df[['slope', 'R2']] = df.apply(g, axis=1)
    

提交回复
热议问题