Reputation: 31
I'm trying to add a new column to a pandas data set that take the numeric month from one column and stores the corresponding season into the new column.
So in essence,
if month == 12, season = 'Winter'
elif month == 2, season = 'Spring'
elif month == 5, season = 'Summer'
else month == 8, season = 'Fall'
I haven't seen a really clear solution for this. I've seen how to do it with 2 conditional values, but I'm still fairly new to pandas and python.
Edit: I was able to get it working with one of the solutions listed below (Thanks!), but I should have mentioned that I also need to include months 1,3,4,6,7,9,10,11
Upvotes: 1
Views: 90
Reputation: 133600
Could you please try following. Fair warning, since no samples of Input and expected output are given so didn't test it.
import pandas as pd
import numpy as np
conditions=[df['month']==12,
df['month']==2,
df['month']==5,
df['month']==8]
choices=['Winter','spring','Summer','fall']
df['season']=np.select(conditions,choices,default=np.nan)
Upvotes: 1
Reputation: 2702
There are some wild solutions in here.
This obviously isn’t a complete example, but it should be sufficient to understand what you need to do:
# map of month number -> season name
months_dict = {12: 'Winter', 2: 'Spring', ...}
df['season'] = df['month_number'].map(months_dict)
Yes, that’s it.
Let me know if you have any questions :)
Upvotes: 1
Reputation: 515
season_map = {12: "Winter",
2: "Spring",
5: "Summer",
8: "Fall"} # Add the rest to this dictionary
df['season'] = df['month_num'].apply(lambda x: season_map[x])
# 'season' is the new column, replace 'month_num' accordingly
Upvotes: -1
Reputation: 325
Since you're already using numpy through pandas, the best way to do this would be through the np.where command.
df['season'] = np.where(df['month']==11|df['month']==12|df['month']==1, 'Winter')
df['season'] = np.where(df['month']==2|df['month']==3|df['month']==4, 'Spring')
df['season'] = np.where(df['month']==5|df['month']==6|df['month']==7, 'Summer')
df['season'] = np.where(df['month']==8|df['month']==9|df['month']==10, 'Fall')
Hope this helps!
Upvotes: -1
Reputation: 143
You can create a dictionary with corresponding numbers and season names, that you can then map onto your new series:
season_dict = {12: "Winter",
2: "Spring",
5: "Summer",
8: "Fall"}
df["season"] = df.month.replace(season_dict)
Upvotes: 1