Here is some example data:
JavaScript
x
20
20
1
mydf = {'Month': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
2
'Freq': [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60]
3
}
4
my_df = pd.DataFrame(mydf, columns=['Month', 'Freq'])
5
my_df
6
7
Month Freq
8
0 1 5
9
1 2 10
10
2 3 15
11
3 4 20
12
4 5 25
13
5 6 30
14
6 7 35
15
7 8 40
16
8 9 45
17
9 10 50
18
10 11 55
19
11 12 60
20
How can I create a new dataframe which groups the months into seasons and find the total sum of each season frequency, while the output is still a dataframe?
I would like something like this: (Winter is where Month = 12, 1, 2)(Spring is where Month = 3, 4, 5)(etc….)
JavaScript
1
6
1
Season Freq
2
0 Winter 75
3
1 Spring 60
4
2 Summer 105
5
3 Autumn 150
6
I have tried to select the rows and concatenate them to start with but I keep getting errors unfortunately.
Advertisement
Answer
You can create a new column with seasons and group on that column:
JavaScript
1
12
12
1
my_df['Season']=df['Month'].apply(lambda x: 'Winter' if x in (12,1,2) else 'Spring' if x in (3,4,5) else 'Summer' if x in (6,7,8) else 'Autumn')
2
3
res=my_df.groupby('Season')['Freq'].sum()
4
5
>>> print(res)
6
7
Season
8
Autumn 150
9
Spring 60
10
Summer 105
11
Winter 75
12