I am trying out this below code. But I am returned with the error
("Newmethod() missing 1 required positional argument: 'B'", 'occurred at index 0')
def Newmethod(A,B):
Add=A+B
return Add
Df=pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]],columns=["A","B","C"])
Df['D']=Df[['A','B']].apply(Newmethod, axis=1)
What do I miss here? Please assist me.
Change it
Df['D']=Df[['A','B']].apply(lambda x:Newmethod(x.A,x.B), axis=1)
A B C D
0 1 2 3 3
1 4 5 6 9
2 7 8 9 15
Explain
You can use np.sum
or change your Newmethod()
. Otherwise, you can only do this in your form.
import numpy as np
Df['D']=Df[['A','B']].apply(np.sum, axis=1)
# or
def Newmethod(x):
Add= x.A + x.B
return Add
Df['D']=Df[['A','B']].apply(Newmethod, axis=1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.