python - How do I apply a function to a pandas dataframe? -
i have tried apply function pandas dataframe 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 returns following error
'the truth value of series ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all().
could me?
thank you
hugo
so first problem in code calling apply , setting param axis=1
applies function row-wise fine.
however, in function referencing whole dataframe when call fogo['tmax'] > 24, why error applying function row-wise trying reference entire dataframe confused.
so change function 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 setting 3 values 3 different conditions use boolean indexing create mask , set rows meet conditions.
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 faster iterating row-wise large dataframes.
so on example data can 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 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]
Comments
Post a Comment