Skip to content Skip to sidebar Skip to footer

Calculating Means For Multiple Columns, In Different Rows In Pandas

I have a csv file like this: -Species- -Strain- -A- -B- -C- -D- Species1 Strain1.1 0.2 0.1 0.1 0.4 Species1 Strain1.1

Solution 1:

IIUC, you simply need to call

df.groupby(['Species', 'Strain']).mean()

                      AB         C    D 
Species   Strain                               
Species1  Strain1.10.20.4666670.1666670.4
          Strain1.20.10.6000000.1000000.3
Species2  Strain2.10.30.3000000.3000000.1
          Strain2.20.40.1500000.5000000.2

What you were doing when you called df.groupby(['Strain','Species']).mean().mean(1) was taking the mean of the 4 means in A, B, C, and D. mean(1) means take the mean over the first axis (i.e. over the columns).

Post a Comment for "Calculating Means For Multiple Columns, In Different Rows In Pandas"