简体   繁体   中英

How to calculate averages and SEM in a multi-indexed pandas dataframe?

I have some data in a pandas dataframe which has a triple multi-index:

Antibody        Time Repeats          
Customer_Col1A2 0    1        0.657532
                     2        0.639933
                     3        0.975302
                5    1        0.628196
                     2        0.663301
                     3        0.921025
                10   1        0.665601
                     2        0.785324
                     3        0.697913

My question is, what is the best way to calculate the average and (sample) standard error of the mean for this data (grouped by time point? So the answer for the 0 time point would be (0.657532+0.639933+0.975302)/3=0.757589 for the average and 0.188750216 for the SD. The output would look something like this:

Antibody        Time Average     sample SD
Customer_Col1A2 0    0.757589    0.188750216
                5    ....        ....
                10   ....        ....

Thanks in advance

You can group by the level of multi-index by specifying the level parameter, and calculate the average and SD using DataFrame.mean() and DataFrame.std() methods correspondingly:

df1.groupby(level=[0,1]).agg({'avg': 'mean', 'sd': 'std'})

在此处输入图片说明

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