且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将pandas函数应用于列以创建多个新列?

更新时间:2021-09-27 09:25:26

基于user1827356的答案,您可以使用df.merge一次完成分配:

Building off of user1827356 's answer, you can do the assignment in one pass using df.merge:

df.merge(df.textcol.apply(lambda s: pd.Series({'feature1':s+1, 'feature2':s-1})), 
    left_index=True, right_index=True)

    textcol  feature1  feature2
0  0.772692  1.772692 -0.227308
1  0.857210  1.857210 -0.142790
2  0.065639  1.065639 -0.934361
3  0.819160  1.819160 -0.180840
4  0.088212  1.088212 -0.911788

请注意巨大的内存消耗和低速: https://ys-l.github.io/posts/2015/08/28/how-not-to-use-pandas-apply/

Please be aware of the huge memory consumption and low speed: https://ys-l.github.io/posts/2015/08/28/how-not-to-use-pandas-apply/ !