简体   繁体   中英

pandas, grouping and aggregating

I have grouped a dataframe:

rwp_initial.df.loc[rwp_initial.df.sample_name=='sma_initial'].groupby(by=['sample_name','pH','salt','column'])['concentration'].plot(marker = 'o', rot=30)

and get following output:

sample_name  pH   salt  column
sma_initial  5.7  50    5         Axes(0.125,0.125;0.775x0.775)
                        6         Axes(0.125,0.125;0.775x0.775)
                  100   7         Axes(0.125,0.125;0.775x0.775)
                        8         Axes(0.125,0.125;0.775x0.775)
                  200   9         Axes(0.125,0.125;0.775x0.775)
                        10        Axes(0.125,0.125;0.775x0.775)
                  400   11        Axes(0.125,0.125;0.775x0.775)
                        12        Axes(0.125,0.125;0.775x0.775)

在此处输入图片说明

i would like to take the mean within each pH and salt concentration. The columns are just the same sample measured two times. If i use aggregate(np.mean) the average of all datapoints of one column is calculated.

This figure maybe highlights the data points i would like to take the average of ( i would like to average along the rows):

rwp_initial.df.loc[rwp_initial.df.sample_name=='sma_initial'].groupby(by=['sample_name','pH','salt'])['concentration'].plot(marker = 'o', rot=30)

OK, i found the answer:

grp_initial = rwp_initial.df.loc[rwp_initial.df.sample_name=='sma_initial'].groupby(by=['sample_name','pH','salt']).concentration

for grp, val in grp_initial:
    print(val.groupby(level='row').aggregate(np.mean))

works

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