1

If I have a dataframe (df) that looks like this:

Date Temperature Climate
4/1  50          Sunny
4/2  55          Cloudy
4/3  48          Rainy
4/4  53          Windy
4/5  33          Snowy
...

and I want to pick out the days with the climate I'm interested in.

climate_of_interest = ['Sunny','Rainy','Snowy']

How can I set up a conditional to get only these climates? I was thinking something like

df_new = df[df['Climate'] in climate_of_interest]

But it doesn't seem to work. Any suggestions? I don't want to write each climate out as its own conditional as that quickly begins to look messy.

Steven Dye
  • 11
  • 2

1 Answers1

1

Here are two options among many.

isin

df[df.Climate.isin(climate_of_interest)]

  Date  Temperature Climate
0  4/1           50   Sunny
2  4/3           48   Rainy
4  4/5           33   Snowy

query

df.query('Climate in @climate_of_interest')

  Date  Temperature Climate
0  4/1           50   Sunny
2  4/3           48   Rainy
4  4/5           33   Snowy
piRSquared
  • 285,575
  • 57
  • 475
  • 624