Accessing columns using MultiIndex after using pandas groupby

I use the df.groupby () method:

g1 = df[['md', 'agd', 'hgd']].groupby(['md']).agg(['mean', 'count', 'std'])

It produces exactly what I want!

         agd                       hgd                
        mean count       std      mean count       std
md                                                    
-4  1.398350     2  0.456494 -0.418442     2  0.774611
-3 -0.281814    10  1.314223 -0.317675    10  1.161368
-2 -0.341940    38  0.882749  0.136395    38  1.240308
-1 -0.137268   125  1.162081 -0.103710   125  1.208362
 0 -0.018731   603  1.108109 -0.059108   603  1.252989
 1 -0.034113   178  1.128363 -0.042781   178  1.197477
 2  0.118068    43  1.107974  0.383795    43  1.225388
 3  0.452802    18  0.805491 -0.335087    18  1.120520
 4  0.304824     1       NaN -1.052011     1       NaN

However, now I want to access the columns of groupby objects, for example, the "normal" file frame.

Then I can: 1) calculate errors using agd and hgd tools 2) draw scatter plots on md (x axis) against the average value of agd (average value of hgd) with the addition of the corresponding error bars.

Is it possible? Perhaps playing with indexing?

Thanks in advance!

+4
source share
2 answers

1) You can rename the columns and continue as usual (get rid of multi-indexing)

g1.columns = ['agd_mean', 'agd_std','hgd_mean','hgd_std']

2) (docs)

g1['agd']['mean count']
+2

, , transform. , , pandas .

0

Source: https://habr.com/ru/post/1653485/


All Articles