简体   繁体   English

为什么我需要lambda将函数应用于Pandas Dataframe?

[英]Why do I need lambda to apply functions to a Pandas Dataframe?

I have a Pandas data frame and am attempting to pass a function over the entries in one column using the apply() function. 我有一个Pandas数据框,并尝试使用apply()函数在一列中的条目上传递函数。

My function is of the form: 我的功能是:

def foo(Y):
    #accepts a pandas data frame
    #carries out some search on the text in each row of the dataframe
    #groups successful searches
    #return a new column as a pandas series

My dataframe is of the form: 我的数据框格形式如下:

    Info    WN    RN
0    XX    YY    ZZ    
1    AA    BB    CC
2    JJ    KK    LL

I attempt to execute: 我试图执行:

df['SR'] = (df['Info'].apply(foo(x)))

My error is as follows: 我的错误如下:

File "<ipython-input-11-ae54015436d8>", line 1, in <module>
df['SR'] = (df['Info'].apply(foo(x))
NameError: name 'x' is not defined

But if I use: 但如果我使用:

df['SR'] = (df['Info'].apply(lambda x:foo(x)))

It works fine. 它工作正常。

I understand how Lambda works (at least I thought I did). 我理解Lambda是如何工作的(至少我以为我做过)。 I don't understand why I need it. 我不明白为什么需要它。

Why do I need lambda to successfully pass the function over the data frame? 为什么我需要lambda才能在数据框上成功传递函数? Shouldn't the apply() function do that by definition? apply()函数不应该按定义执行吗?

Or is it that I am effectively doing it the other way around ie passing my data frame into the function, and returning some output, rather than iteratively applying the function to the data frame (if that makes sense)? 或者是我有效地做其他方式,即将我的数据框传递给函数,并返回一些输出,而不是迭代地将函数应用于数据框(如果这是有意义的)?

Can anyone offer any insight? 有人可以提供任何见解吗?

My sincere thanks! 衷心的感谢!

The lambda is unnecessary, you can just do lambda是不必要的,你可以这样做

df['SR'] = df['Info'].apply(foo)

here it will still work 在这里它仍然有效

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

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