Search code examples
pythonpandasaggregate

Grouping by Multi-Indices of both Row and Column


I have created a table using Pandas following material from here.

The table created makes use of Multi-Indices for both columns and rows. enter image description here

I am trying to compute the descriptive statistics for each year and subject, meaning, displaying for instance the mean of 2013 for Bob, the mean for 2013 for Guido, and the mean for 2013 for Sue, for all subjects, and for all years. The means for Bob would consider the means for HR and Temp. Note: The types are the same as a coincidence, as this is not the case for the table implemented. Other subjects not included in the screenshot have varying types.

The closest I have managed to come to the solution is through the following code df.groupby(level = 0, axis = 0).describe() This grouped the data by the year, however, did not group by subject also.


Solution

  • Providing links to external websites is also discouraged as they may change/disappear at any time without SO control

    Having said that, the link provides most of the tools you need to answer your questions. More specifically, a combination of stack and mean should give you what you specifically asked about:

    health_data.stack().mean(level = 'year')
    

    produces

    
    subject Bob     Guido   Sue
    year            
    2013    28.4    40.400  34.15
    2014    43.2    38.025  41.10
    

    or more generally

    health_data.stack().groupby('year').describe()
    

    produces a long dataframe with the statistics grouped by year, for each subject:

    subject Bob                                     Guido                       Sue
    count   mean    std min 25% 50% 75% max count   mean    ... 75% max count   mean    std min 25% 50% 75% max
    year                                                                                    
    2013    4.0 28.4    11.580443   13.0    22.75   31.3    36.95   38.0    4.0 40.400  ... 42.500  50.0    4.0 34.15   4.297674    30.0    30.75   33.95   37.35   38.7
    2014    4.0 43.2    7.566593    36.4    37.15   42.2    48.25   52.0    4.0 38.025  ... 39.875  44.0    4.0 41.10   12.961996   28.0    35.65   38.70   44.15   59.0