pandas, apply with args which are dataframe row entries

匿名 (未验证) 提交于 2019-12-03 08:46:08

问题:

I have a pandas dataframe 'df' with two columns 'A' and 'B', I have a function with two arguments

def myfunction(B, A):     # do something here to get the result     return result 

and I would like to apply it row-by-row to df using the 'apply' function

df['C'] = df['B'].apply(myfunction, args=(df['A'],)) 

but I get the error

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). 

whats happening here, it seems it takes df['A'] as the whole series! not just the row entry from that series as required.

回答1:

I think you need:

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 

Or:

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 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!