Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying arguments to pandas aggregate function

I have following code:

grouped[['scene_average']].agg([np.mean, np.std, len])

I want to call np.std with ddof=5

How I can do so ?

like image 677
Night Walker Avatar asked Jun 06 '26 15:06

Night Walker


1 Answers

Use lambda function:

grouped[['scene_average']].agg([np.mean, lambda x: np.std(x, ddof=5), len])

Another solution with pandas function mean, std:

grouped[['scene_average']].agg(['mean', lambda x: x.std(ddof=5), len])

Sample:

np.random.seed(10)
df = pd.DataFrame({'scene_average':np.random.randint(10,size=20), 
                   'a':np.random.randint(3,size=20)})
grouped = df.groupby('a')

df1 = grouped[['scene_average']].agg([np.mean, lambda x: np.std(x, ddof=5), len])
d = {'<lambda>':'std','len':'count'}
df1 = df1.rename(columns = d)
print (df1)
  scene_average                
           mean       std count
a                              
0      5.500000       NaN     4
1      3.555556  5.749396     9
2      5.000000  4.898979     7

And if remove MultiIndex in columns:

df1.columns = df1.columns.map('_'.join)
print (df1)
   scene_average_mean  scene_average_std  scene_average_count
a                                                            
0            5.500000                NaN                    4
1            3.555556           5.749396                    9
2            5.000000           4.898979                    7
like image 158
jezrael Avatar answered Jun 08 '26 05:06

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!