我有这个pandas DataFrame df
:
df.head()
windIntensity year month day hour AOBT delay
3 2015 1 1 0 0 0.0 15.0
2 2015 1 1 0 0 0.0 10.0
2 2015 1 1 1 0 0.0 5.0
2 2015 1 1 1 0 0.0 0.0
1 2015 1 1 2 0 0.0 0.0
执行此代码时:
df = dfj.groupby(["year","hour"]).agg({'windIntensity':'mean','delay':['mean','count']}).reset_index()
我得到了这个结果:
year hour windIntensity delay
mean mean count
0 2015 0 4.239207 24.240373 857
1 2015 1 4.029024 15.770449 758
2 2015 2 3.863928 7.431322 779
3 2015 3 3.859801 4.161290 806
4 2015 4 3.782659 4.722230 6851
但是如何重命名列以获得一行列,而不是两行?
预期结果:
year hour windIntensity_mean delay_mean count
0 2015 0 4.239207 24.240373 857
1 2015 1 4.029024 15.770449 758
2 2015 2 3.863928 7.431322 779
3 2015 3 3.859801 4.161290 806
4 2015 4 3.782659 4.722230 6851
答案 0 :(得分:1)
演示:
具有多级列的源DF:
In [223]: r
Out[223]:
year hour windIntensity delay
mean mean count
0 1 0 2015 6.0 5
溶液:
In [224]: r.columns = r.columns.map(lambda c: ('_' if c[1] else '').join(c))
结果:
In [225]: r
Out[225]:
year hour windIntensity_mean delay_mean delay_count
0 1 0 2015 6.0 5