Home > Enterprise >  How to create a column whose values are based on the values of another column?
How to create a column whose values are based on the values of another column?

Time:09-27

I have a df like this:

Year        2016    2017    
Month               
1       0.979000    1.109000    
2       0.974500    1.085667    
3       1.004000    1.075667    
4       1.027333    1.184000    
5       1.049000    1.089000    
6       1.013250    1.085500    
7       0.999000    1.059000    
8       0.996667    1.104000    
9       1.024000    1.121333    
10      1.019000    1.126333    
11      0.949000    1.183000    
12      1.074000    1.203000    

How can I add a 'Season' column that populates "Spring", "Summer" etc. based on the numerical value of month? E.g months 12, 1, and 2 = Winter, etc?

CodePudding user response:

You could iterate through the column, appending data to a new data frame which you will add in as a column.

for i in df['Year Month'] :
    if i == 12 or 1 or 2 :
        i = "Winter"
        df2.append(i)

Then add on your other conditions with elif and else statements and you should be good to add it onto your main df afterwards. Lemme know if this helps.

CodePudding user response:

You could use np.select with pd.Series.between:

import numpy as np
df["Season"] = np.select([df["Month"].between(3, 5), 
                          df["Month"].between(6, 8),
                          df["Month"].between(9, 11)], 
                          ["Spring", "Summer", "Fall"], 
                          "Winter")

    Month      2016      2017  Season
0       1  0.979000  1.109000  Winter
1       2  0.974500  1.085667  Winter
2       3  1.004000  1.075667  Spring
3       4  1.027333  1.184000  Spring
4       5  1.049000  1.089000  Spring
5       6  1.013250  1.085500  Summer
6       7  0.999000  1.059000  Summer
7       8  0.996667  1.104000  Summer
8       9  1.024000  1.121333    Fall
9      10  1.019000  1.126333    Fall
10     11  0.949000  1.183000    Fall
11     12  1.074000  1.203000  Winter
  • Related