Add a row with means of columns to pandas DataFrame

Use loc for setting with enlargement:

df.loc['mean'] = df.mean()

The resulting output:

       diode1  diode2  diode3  diode4
Time                                 
0.53      7.0     0.0    10.0    16.0
1.218    17.0     7.0    14.0    19.0
1.895    13.0     8.0    16.0    17.0
2.57      8.0     2.0    16.0    17.0
3.24     14.0     8.0    17.0    19.0
3.91     13.0     6.0    17.0    18.0
4.594    13.0     5.0    16.0    19.0
5.265     9.0     0.0    12.0    16.0
5.948    12.0     3.0    16.0    17.0
6.632    10.0     2.0    15.0    17.0
mean     11.6     4.1    14.9    17.5

Leave a Comment