Run*_*ean 7 python args apply pandas
我有一个pandas数据帧'df',有两列'A'和'B',我有一个带有两个参数的函数
def myfunction(B, A):
# do something here to get the result
return result
Run Code Online (Sandbox Code Playgroud)
我想使用'apply'函数逐行将它应用到df
df['C'] = df['B'].apply(myfunction, args=(df['A'],))
Run Code Online (Sandbox Code Playgroud)
但我得到了错误
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Run Code Online (Sandbox Code Playgroud)
什么发生在这里,似乎需要df ['A']作为整个系列!不仅仅是该系列中的行条目.
jez*_*ael 16
我想你需要:
import pandas as pd
df = pd.DataFrame({'A':[1,2,3],
'B':[4,5,6]})
print (df)
A B
0 1 4
1 2 5
2 3 6
def myfunction(B, A):
#some staff
result = B + A
# do something here to get the result
return result
df['C'] = df.apply(lambda x: myfunction(x.B, x.A), axis=1)
print (df)
A B C
0 1 4 5
1 2 5 7
2 3 6 9
Run Code Online (Sandbox Code Playgroud)
要么:
def myfunction(x):
result = x.B + x.A
# do something here to get the result
return result
df['C'] = df.apply(myfunction, axis=1)
print (df)
A B C
0 1 4 5
1 2 5 7
2 3 6 9
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8016 次 |
| 最近记录: |