简体   繁体   English

pandas,应用参数为 dataframe 行条目

[英]pandas, apply with args which are dataframe row entries

I have a pandas dataframe 'df' with two columns 'A' and 'B', I have a function with two arguments我有一个 pandas dataframe 'df' 有两列 'A' 和 'B',我有一个 function 有两个 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 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.这里发生了什么,它似乎需要 df['A'] 作为整个系列。 not just the row entry from that series as required.不仅仅是该系列的行条目。

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

I would add one more method, which could be useful when you need pass all columns to the function.我会再添加一种方法,当您需要将所有列传递给 function 时,它会很有用。

We consider that according to pandas.DataFrame.apply docs: Objects passed to the function are Series objects .我们认为根据pandas.DataFrame.apply文档:传递给 function 的对象是系列对象 So we convert pd.Series to list .所以我们将pd.Series转换为list Then unpack it using * operator, while our function call.然后使用*运算符将其解压缩,同时调用 function。

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.to_list()), axis=1)
print (df)
   A  B  C
0  1  4  5
1  2  5  7
2  3  6  9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM