Cyrine Ezzahra Cyrine Ezzahra - 3 months ago 8
Python Question

Mean of sum in a DataFrame with Python

I create a DataFrame df1 which contains for each day of the week the number of activation time for each machine.

machine1 38696 non-null float64
machine3 38697 non-null float64
machine5 38695 non-null float64
machine6 38695 non-null float64
machine7 38693 non-null float64
machine8 38696 non-null float64
date 38840 non-null datetime64[ns]
day_of_week 38840 non-null object
dtypes: datetime64[ns](2), float64(6), object(1)
memory usage: 2.7+ MB



Machine1 Machine3 Machine5 Machine6 Machine7 Machine8 date day_of_week
90.0 90.0 90.0 90.0 90.0 90.0 2015-07-31 Fri
0.0 0.0 0.0 0.0 0.0 0.0 2015-07-31 Mon
0.0 0.0 0.0 0.0 0.0 0.0 2015-07-31 Tues
0.0 0.0 0.0 0.0 0.0 0.0 2015-07-31 Fri
0.0 0.0 0.0 0.0 0.0 0.0 2015-07-31 Tues


I try to create another DataFrame which extracts for each machine the mean of activation per day. For example:

Machine1 Machine3 Machine5 Machine6 Machine7 Machine8
Mon 0 .. .. .. .. ..
Tue 0
wed 0
thu 0
fri 45


Can you help me to achieve this in the smartest way?

Answer

IIUC you can use:

print (df.groupby('day_of_week').mean())
             Machine1  Machine3  Machine5  Machine6  Machine7  Machine8
day_of_week                                                            
Fri              45.0      45.0      45.0      45.0      45.0      45.0
Mon               0.0       0.0       0.0       0.0       0.0       0.0
Tues              0.0       0.0       0.0       0.0       0.0       0.0

If need output with reseting index:

print (df.groupby('day_of_week', as_index=False).mean())
  day_of_week  Machine1  Machine3  Machine5  Machine6  Machine7  Machine8
0         Fri      45.0      45.0      45.0      45.0      45.0      45.0
1         Mon       0.0       0.0       0.0       0.0       0.0       0.0
2        Tues       0.0       0.0       0.0       0.0       0.0       0.0