Suppose I have a df
which has columns of 'ID', 'col_1', 'col_2'
. And I define a function :
f = lambda x, y : my_function_expression.
Now I want to apply the f
to df
's two columns 'col_1', 'col_2'
to element-wise calculate a new column 'col_3'
, somewhat like :
df['col_3'] = df[['col_1','col_2']].apply(f)
How to do ?
译文:怎么同时对列 col_1 和 col_2 执行map操作,生成新的一列?
答:
Here's an example using apply
on the dataframe, which I am calling with axis = 1
.
Note the difference is that instead of trying to pass two values to the function f
, rewrite the function to accept a pandas Series object, and then index the Series to get the values needed.
In [49]: df Out[49]: 0 1 0 1.000000 0.000000 1 -0.494375 0.570994 2 1.000000 0.000000 3 1.876360 -0.229738 4 1.000000 0.000000 In [50]: def f(x): ....: return x[0] + x[1] ....: In [51]: df.apply(f, axis=1) #passes a Series object, row-wise Out[51]: 0 1.000000 1 0.076619 2 1.000000 3 1.646622 4 1.000000
Depending on your use case, it is sometimes helpful to create a pandas group
object, and then use apply
on the group.
译文:利用apply函数,在apply函数参数处指定自定义函数.自定义函数同时对多列进行计算,返回计算结果即可,详见代码.