-1

I'm working with frames and I'm trying to transform a variable where months and dates are strings into a new variable season. However my if conditional statements below will only make a new var season where everything is spring (probably because it's the first if statement), everything else is ignored.

Anyone got any clue as to why?

if 'september' or 'october' or 'november' in labels['str_date']:
    labels['season'] = 'spring'
if 'december' or 'january' or 'febuary' in labels['str_date']:
    labels['season'] = 'summer'
if 'march' or 'april' or 'may' in labels['str_date']:
    labels['season'] = 'autumn'
if 'june' or 'july' or 'august' in labels['str_date']:
    labels['season'] = 'winter'
else: 
    labels['season'] = 'null'
    
#labels.loc[(labels.season == 'spring')].value_counts()
#labels
sns.countplot(x='season', data=(labels))
plt.show() 

enter image description here

subset of the df = labels:

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
Helena
  • 69
  • 6
  • you cannot do `if a or b in list`. Because 'or' and 'and' must be used between booleans like 'true or true'. So you have to write a full sentence `if a in list or b in list` – Flavio Moraes Sep 09 '21 at 17:15

1 Answers1

2

That's not the way or or in work. This statement is processed as

if ('september') or ('october') or ('november' in labels['str_date']):

Non-empty strings are treated as True values, and anything OR-ed with True is True:

if True or True or ('november' in labels['str_date']):

You can use the following:

month = labels['str_date').split()[0]

if month in ('september', 'october', 'november'):
    labels['season'] = 'spring'
...
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251