我创建一个DataFrame df1,其中包含一周中每一天的每一台计算机的激活时间。
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
我尝试创建另一个DataFrame,为每个计算机提取每天激活的平均值。例如:
Machine1 Machine3 Machine5 Machine6 Machine7 Machine8
Mon 0 .. .. .. .. ..
Tue 0
wed 0
thu 0
fri 45
您能帮助我以最聪明的方式实现这一目标吗?
您可以使用IIUC:
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
如果需要带有复位索引的输出:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句