0

I have this 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

When I execute this code:

df = dfj.groupby(["year","hour"]).agg({'windIntensity':'mean','delay':['mean','count']}).reset_index()

I get this result:

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

But how can I rename columns to get one line of column, not two lines?

Expected result:

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
ScalaBoy
  • 3,254
  • 13
  • 46
  • 84

1 Answers1

1

Demo:

source DF with multi-level columns:

In [223]: r
Out[223]:
  year hour windIntensity delay
                     mean  mean count
0    1    0          2015   6.0     5

solution:

In [224]: r.columns = r.columns.map(lambda c: ('_' if c[1] else '').join(c))

result:

In [225]: r
Out[225]:
   year  hour  windIntensity_mean  delay_mean  delay_count
0     1     0                2015         6.0            5
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419