I have this pandas dataframe including a column for months whose type is int64, and I want to change every value in the column to be the corresponding season of the year.
For example if x is any of 12, 1, or 2, change the value of x to 'winter'
, etc.
I have tried some for loops but no use. I think there is a one liner that can do this using lambda x
.
I supposed that every month has only 1 season so for example winter is in months 12, 1, and 2, and so on.
For example (0, 1, and 2 are indices):
input:
0 1
1 3
2 6
output:
0 winter
1 spring
2 summer
My problem is with month 12 or else I could have used bins.
Any ideas?
CodePudding user response:
One fast and flexible way is to map
a dict from current column values to new column values:
d = dict(zip(range(1,13), ["winter"]*2 ["spring"]*3 ["summer"]*3 ["fall"]*3 ["winter"]))
df = pd.DataFrame(dict(month=[3,4,6,1,5,6,2,4,5]))
df = df.month.map(d)
output:
0 spring
1 spring
2 summer
3 winter
4 spring
5 summer
6 winter
7 spring
8 spring
Name: month, dtype: object
CodePudding user response:
then u can easily write this function
def map_fn(x: int):
if x in range(3, 6):
return "spring"
if x in range(6, 9):
return "summer"
if x in range(9, 12):
return "autumn"
return "winter"
and then apply it to your column like this
df.seasons.apply(map_fn)
to have this
CodePudding user response:
I solved it using pandas replace and cut methods:
df['month']=df['month'].replace(12,0)
bin_edges = [-1,2,5,8,11]
bin_labels= ["winter",'spring','summer','fall']
df['month']=pd.cut(df['month'], bin_edges, labels=bin_labels)
Thanks all.