Home > Back-end >  How to group by Month as Abbreviated month name
How to group by Month as Abbreviated month name

Time:01-21

I have the following time series with hourly data of several years:

    local time         ghi mean
0       2013-01-01 00:00:00 0.0
1       2013-01-01 01:00:00 0.0
2       2013-01-01 02:00:00 0.0
3       2013-01-01 03:00:00 0.0
4       2013-01-01 04:00:00 0.0
..         ...          ...
8754    2016-12-31 18:00:00 427.5
8755    2016-12-31 19:00:00 194.9
8756    2016-12-31 20:00:00 116.5
8757    2016-12-31 21:00:00 237.6
8758    2016-12-31 22:00:00 113.8
8759    2016-12-31 23:00:00 0.0

I want to calculate the average values per month of those.The desired output would be something similar to:

    local time     ghi mean
0     January    769.187097
1     February   756.828125
..         ...          ...
11    November   491.085618
12    December   859.930933

I've already tried :

df.groupby(pd.PeriodIndex(df['local time'], freq="M"))['ghi mean'].mean().reset_index()

But got :

    local time     ghi mean
0      2009-01   769.187097
1      2009-02   756.828125
2      2009-03   747.142339
3      2009-04   661.948194
4      2009-05   491.085618
..         ...          ...
127    2019-08   859.930933
128    2019-09   852.150131
129    2019-10  1239.419635
130    2019-11  1072.209155
131    2019-12  1255.293607

CodePudding user response:

Group by the month name:

out = df.groupby(df['local time'].dt.strftime('%B'), sort=False).mean().reset_index()
print(out)

# Output:
  local time    ghi mean
0    January    0.000000
1   December  181.716667
  •  Tags:  
  • Related