change season in pandas
Question:
I have a dataset like this
Month
Season
1
1
2
1
3
2
4
2
5
2
6
3
7
3
8
3
9
4
10
4
11
4
12
1
Now I would like to change 1 to winter, 2 to spring, 3 to summer and 4 to autumn in the season column.
I was struggling.
I want this dataset after
Month
Season
1
winter
2
winter
3
spring
4
spring
5
spring
6
summer
7
summer
8
summer
9
autumn
10
autumn
11
autumn
12
winter
I gave the following code
df.replace({'Season' : { 1 : winter, 2 : spring, 3: summer, 4: autumn}})
But got an error
The error is could not find winter???
Answers:
# create a dictionary for season
d={1:'winter', 2 : 'spring', 3 : 'summer', 4 : 'autumn'}
# map season value using dictionary
df['Season']=df['Season'].map(d)
df
Month Season
0 1 winter
1 2 winter
2 3 spring
3 4 spring
4 5 spring
5 6 summer
6 7 summer
7 8 summer
8 9 autumn
9 10 autumn
10 11 autumn
11 12 winter
Alternately, for completeness sake. you can update your solution of using replace as follows (as others have commented here), which results in the same result
df.replace({'Season' : { 1 : 'winter', 2 : 'spring', 3: 'summer', 4: 'autumn'}})
Put the seasons like winter, summer etc in quotes. For example, winter
becomes "winter"
The problem is that winter is treated as a variable, not a string.
This should help with the winter not found error
.
Would suggest apply
approach:
def month_to_season(row):
if (row + 1) // 3 == 1:
return 'spring'
if (row + 1) // 3 == 2:
return'summer'
if (row + 1) // 3 == 3:
return 'autumn'
return 'winter'
df['Season'] = df['Month'].apply(month_to_season)
I have a dataset like this
Month | Season |
---|---|
1 | 1 |
2 | 1 |
3 | 2 |
4 | 2 |
5 | 2 |
6 | 3 |
7 | 3 |
8 | 3 |
9 | 4 |
10 | 4 |
11 | 4 |
12 | 1 |
Now I would like to change 1 to winter, 2 to spring, 3 to summer and 4 to autumn in the season column.
I was struggling.
I want this dataset after
Month | Season |
---|---|
1 | winter |
2 | winter |
3 | spring |
4 | spring |
5 | spring |
6 | summer |
7 | summer |
8 | summer |
9 | autumn |
10 | autumn |
11 | autumn |
12 | winter |
I gave the following code
df.replace({'Season' : { 1 : winter, 2 : spring, 3: summer, 4: autumn}})
But got an error
The error is could not find winter???
# create a dictionary for season
d={1:'winter', 2 : 'spring', 3 : 'summer', 4 : 'autumn'}
# map season value using dictionary
df['Season']=df['Season'].map(d)
df
Month Season
0 1 winter
1 2 winter
2 3 spring
3 4 spring
4 5 spring
5 6 summer
6 7 summer
7 8 summer
8 9 autumn
9 10 autumn
10 11 autumn
11 12 winter
Alternately, for completeness sake. you can update your solution of using replace as follows (as others have commented here), which results in the same result
df.replace({'Season' : { 1 : 'winter', 2 : 'spring', 3: 'summer', 4: 'autumn'}})
Put the seasons like winter, summer etc in quotes. For example, winter
becomes "winter"
The problem is that winter is treated as a variable, not a string.
This should help with the winter not found error
.
Would suggest apply
approach:
def month_to_season(row):
if (row + 1) // 3 == 1:
return 'spring'
if (row + 1) // 3 == 2:
return'summer'
if (row + 1) // 3 == 3:
return 'autumn'
return 'winter'
df['Season'] = df['Month'].apply(month_to_season)