1

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

3
  • I am assuming you have a big df with many month entries. If you make a df with month-season (aka df.shape=(2,12)) then you can do a join on the month column. Commented Nov 29, 2019 at 19:04
  • What have you done to try to solve this? Have you tried anything? Commented Nov 30, 2019 at 2:56
  • Also, which solution did you manage to get working? I believe mine is now the simplest amongst them. Commented Nov 30, 2019 at 3:01

5 Answers 5

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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 :)

Comments

1

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)

8 Comments

That did work, yes. Just had to add duplicates of each choice to correspond to the months I didn't explicitly list.
Heads up, there’s a mismatched quote in there.
@AlexanderCécile, thanks buddy for letting know, I fixed it now, Thanks a TON.
@Michael, Glad that it helped you. Give it sometime and when you have few answers to your question you could select "anyone" of them as correct one too, cheers.
@AlexanderCécile, Hi Alex, apologies if you are not the down voter, in case you have given down vote then please do lemme know the reason for same, thank you.
|
-1

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!

3 Comments

Isn’t numpy.where() completely unnecessary here?
I mean, it's an approach... yours is definitely a bit simpler though :) Wasn't familiar with the .map commands from my limited experience.
The general approach is fine, what I meant is that where() is designed to choose between two alternatives. That first line, for example, could be written df.loc[df['month'] == 1 | df['month'] == 11 | df['month'] == 12, 'season'] = 'Winter', or even simpler df.loc[df['month'].isin({1, 11, 12}), 'season'] = 'Winter'.
-1
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

3 Comments

df['season'] = df['month_num'].apply(lambda x: season_map[x]) ---> df['season'] = df['month_num'].map(season_map)
I fail to understand why my answer has been downvoted. An accompanying explanation as to where my code is wrong would be welcome and helpful.
See my comment above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.