import numpy as np
df = pd.DataFrame ({'a' : np.random.randn(6),
'b' : ['foo', 'bar'] * 3,
'c' : np.random.randn(6)})
def my_test(a, b):
return a + b
df['Value'] = df.apply(lambda row: my_test(row['a'], row['c']), axis=1)
print (df)
#输出结果形如:
# a b c Value
# 0 -0.276507 foo -3.122166 -3.398672
# 1 -0.589019 bar -1.150915 -1.739934
# 2 -0.485433 foo 1.296634 0.811200
# 3 0.469688 bar -0.554992 -0.085304
# 4 1.297845 foo 1.672957 2.970802
# 5 -0.702724 bar -1.609585 -2.312309
转载大佬文章,地址出处:https://blog.youkuaiyun.com/leokingszx/article/details/78266559