简体   繁体   English

如何将函数应用于熊猫数据框?

[英]How do I apply a function to a pandas dataframe?

I have tried to apply a function to a pandas dataframe like this 我试图将函数应用于这样的熊猫数据框

fogo = intervalo.resample('D', how = ['max']).TMP
fogo['Tmin'] = intervalo.resample('D', how = ['min']).TMP
fogo['Rain'] = intervalo.resample('D', how = ['sum']).RNF
fogo.columns = ['TMax','TMin','Rain']
fogo['Fogo'] = (fogo['TMax']>24) \
        | ((fogo['TMax']>21) & (fogo['TMin']>12)) \
        | ((fogo['TMax']>18) & (fogo['TMin']>10) & (fogo['Rain']>2))

def f(x):    
if (fogo['TMax']>24):
    return 'a'
elif ((fogo['TMax']>21) & (fogo['TMin']>12)):
    return 'b'
elif ((fogo['TMax']>18) & (fogo['TMin']>10) & (fogo['Rain']>2)):
    return 'c'

fogo['Causa'] = fogo.apply(f, axis=1)


    TMax    TMin    Rain    Fogo            Causa
2012-04-01   21.6    10.3    0.8     False    empty
2012-04-02   19.3    9.5     0.0     False    empty
2012-04-03   16.2    10.1    0.2     False    empty
2012-04-04   16.7    11.4    0.2     False    empty
2012-04-05   14.0    5.9     2.9     False    empty

but it returns the following error 但它返回以下错误

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

Could you help me? 你可以帮帮我吗?

Thank you 谢谢

Hugo 雨果

So the first problem in your code is that you are calling apply and setting param axis=1 this applies your function row-wise which is fine. 因此,代码中的第一个问题是您正在调用Apply并设置param axis=1这将按行应用函数,这很好。

However, in your function you are referencing the whole dataframe when you call fogo['TMax'] > 24, this is why you get the error as you are applying your function row-wise but then trying to reference the entire dataframe so it confused. 但是,在函数中,当您调用fogo ['TMax']> 24时,您正在引用整个数据帧,这就是为什么在逐行应用函数但尝试引用整个数据帧时会收到错误的原因。

So you could change your function to this: 因此,您可以将函数更改为此:

def f(x):    
    if (x['TMax']>24):
        return 'a'
    elif ((x['TMax']>21) & (x['TMin']>12)):
        return 'b'
    elif ((x['TMax']>18) & (x['TMin']>10) & (x['Rain']>2)):
        return 'c'

However, seeing as you are just setting three values for 3 different conditions then you could just use boolean indexing to create a mask and just set all rows that meet your conditions. 但是,看到您只是为3个不同的条件设置了三个值,那么您就可以使用布尔索引创建掩码,并设置所有符合条件的行。

so: 所以:

fogo.loc[fogo['TMax']> 24,'Causa'] = 'a'
fogo.loc[(fogo['TMax']> 21) & (fogo['TMin'] > 12),'Causa'] = 'b'
fogo.loc[(fogo['TMax']> 18) & (fogo['TMin'] > 10) & (fogo['Rain'] > 2),'Causa'] = 'c'

This will be much faster then iterating row-wise especially for large dataframes. 这将比逐行迭代要快得多,尤其是对于大型数据帧而言。

So on your example data I can do this: 因此,在您的示例数据上,我可以执行以下操作:

In [10]:

fogo.loc[fogo['TMax']> 21,'Causa'] = 'a'
fogo.loc[(fogo['TMax']> 21) & (fogo['TMin'] > 11),'Causa'] = 'b'
fogo.loc[(fogo['TMax']> 11) & (fogo['TMin'] > 5) & (fogo['Rain'] > 2),'Causa'] = 'c'
fogo
Out[10]:
            TMax  TMin  Rain   Fogo  Causa
2012-04-01  21.6  10.3   0.8  False      a
2012-04-02  19.3   9.5   0.0  False  empty
2012-04-03  16.2  10.1   0.2  False      b
2012-04-04  16.7  11.4   0.2  False      b
2012-04-05  14.0   5.9   2.9  False      c

[5 rows x 5 columns]

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

相关问题 In Pandas, how do I apply a function to a row of a dataframe, where each item in the row should be passed to the function as an argument? - In Pandas, how do I apply a function to a row of a dataframe, where each item in the row should be passed to the function as an argument? 如何将为一个数字设计的函数应用于大熊猫中的整个数据框? - How do I apply a function designed for one number to an entire dataframe in pandas? 如何将返回 pandas dataframe 的 function 应用于一系列输入,以便返回单个数据帧? - How do I apply my function which returns a pandas dataframe, to a range of inputs so it returns individual dataframes? 如何将 function 应用于 pandas dataframe 中的每一行? - How can I apply a function to each row in a pandas dataframe? 如何将“硬币兑换问题”应用于 pandas dataframe? - How do I apply the “coin changing problem” to a pandas dataframe? 如何在 Pandas 数据框上正确应用 group by? - How do I correctly apply group by on pandas dataframe? 在Pandas数据帧上应用函数 - Apply function on Pandas dataframe 熊猫数据框apply()函数 - Pandas Dataframe apply() function 在熊猫数据框中应用功能 - Apply function in a pandas dataframe 如何将功能应用于多个熊猫数据框 - How to apply function to multiple pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM