I have a pandas dataframe ‘df’ with two columns ‘A’ and ‘B’, I have a function with two arguments
JavaScript
x
4
1
def myfunction(B, A):
2
# do something here to get the result
3
return result
4
and I would like to apply it row-by-row to df using the ‘apply’ function
JavaScript
1
2
1
df['C'] = df['B'].apply(myfunction, args=(df['A'],))
2
but I get the error
JavaScript
1
2
1
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
2
whats happening here, it seems it takes df[‘A’] as the whole series! not just the row entry from that series as required.
Advertisement
Answer
I think you need:
JavaScript
1
23
23
1
import pandas as pd
2
df = pd.DataFrame({'A':[1,2,3],
3
'B':[4,5,6]})
4
5
print (df)
6
A B
7
0 1 4
8
1 2 5
9
2 3 6
10
11
def myfunction(B, A):
12
#some staff
13
result = B + A
14
# do something here to get the result
15
return result
16
17
df['C'] = df.apply(lambda x: myfunction(x.B, x.A), axis=1)
18
print (df)
19
A B C
20
0 1 4 5
21
1 2 5 7
22
2 3 6 9
23
Or:
JavaScript
1
13
13
1
def myfunction(x):
2
3
result = x.B + x.A
4
# do something here to get the result
5
return result
6
7
df['C'] = df.apply(myfunction, axis=1)
8
print (df)
9
A B C
10
0 1 4 5
11
1 2 5 7
12
2 3 6 9
13