简体   繁体   中英

Custom function in Pandas aggregation

I have a following dataframe - result from this question :

                         t2m  ...       kont    sum             d1                  d2
latitude longitude            ...           
46.5     18.0       0.284698  ...               0.001613        1998-01-12 07:00:00 1998-01-24 08:00:00
         18.0      -1.304504  ...   FROMHERE    0.004097        1998-01-24 08:00:00 1998-01-24 09:00:00
         18.0       0.345001  ...   FROMHERE    0.024207        1998-01-24 17:00:00 1998-01-25 00:00:00         
         18.0      -4.786346  ...   FROMHERE    xxxxxx

I want to implement combination of custom and build in functions to .agg of this dataframe. Here is the code:

dfgeo=df.groupby(['latitude', 'longitude']).agg(
                 std=('sum',np.std),
                 maks=('sum','max'),
                 mean=('sum',(lambda x: mean(absolute(x - mean(x)))))
                 ).reset_index()

Code mean=('sum',(lambda x: mean(absolute(x - mean(x))))) mimics Mean Average Deviation since is not directly build in Numpy, or i cant find it. I get following error:

KeyError: "[('ar', '<lambda>')] not in index"

Any help is appreciated.

For me working custom function:

def f(x):
    return np.mean(np.abs(x - np.mean(x)))

dfgeo=df.groupby(['latitude', 'longitude']).agg(
                 std=('sum',np.std),
                 maks=('sum','max'),
                 mean=('sum',f)
                 ).reset_index()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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