将函数应用于以其他列作为参数的pandas列 [英] Apply function to pandas column having other column as argument
本文介绍了将函数应用于以其他列作为参数的pandas列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿,我正在尝试使用 y作为参数将函数应用于列 x ,因此对于要应用 x 的每一行改为 y :
Hey I am trying to apply a function to a column x using y as parameter, so for every row I want to apply x to y, something like this:
def fun(x, y):
return x + y
df['xy'] = df['x'].apply(fun, args = df['y'])
但是它不起作用.有任何想法吗?请注意,我的实函数并不像添加两个值一样简单
But it does not work. Any ideas? Note that my real function is not as simple as adding two values
推荐答案
But if possible, better is use vectorized operations, here:
df['xy'] = df['x'] + df['y']
这篇关于将函数应用于以其他列作为参数的pandas列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文