首页 > 解决方案 > Python:将代码应用于整个数据框列

问题描述

我在一个填充有值的数据框中有一列 col1。

                                col1                
row1      [0.0, 6.33839991, 3.93961207, 5.27702178, 8.27702178, 6.44343, 5.668574]             
row2      [0.0, 5.93961207, 4.27702178, 4.12702178]
row3      [0.0, 6.44428501, 3.93961207, 8.27702178, 4.27121178]

每行包含一个系列。我需要一个新列,比如 col2,它返回一个新系列。该系列从 0 开始,每次递增 100,直到该系列与原始系列的长度相匹配。

预期产出

                            col1                               col2          
row1      [0.0, 6.3, 3.93, 5.27, 8.2, 6.4, 5.6]             [0,100,200,300,400,500,600]                       
row2      [0.0, 5.9, 4.2, 4.1]                              [0,100,200,300]
row3      [0.0, 6.4, 3.9, 8.2, 4.2]                         [0,100,200,300,400]

谢谢!

标签: pythonpandasnumpydataframe

解决方案


我的主张是:

df['col2'] = df.col1.transform(lambda x: [ i * 100 for i in range(len(x))])

推荐阅读