How to use Multiple Condition with Pandas DataFrame

pandas
dataframe

#1

Hello,

I have a small DataFrame object which has the following Features:

  1. Day
  2. Temperature
  3. WindSpeed
  4. Event (Sunny, Cloudy, Snow, Rain)

I want to list “Day” and “WIndSpeed” where “WindSpeed” >4 “OR” “Temperature” >30

I am using the following command to the execute the above condition, however, its misfiring.

df[["Day","WindSpeed"]] [df["WindSpeed"]>4 | df["Temperature"]>30] 

Can someone tell me what’s wrong with the above command?

I don’t want to use “.loc” or “.iloc”

Thanks
Mohit


#2

Hi @mohitlearns,

You can try out something like:

new_df = df[(df['WindSpeed']>4) & (df['Temperature']>30)]

This will give you a new dataframe where WindSpeed for each row will be greater than 4 and Temperature will be greater than 30. Now you can select the required columns from new_df as per your requirement.


#3

Thanks! But what if I just want to list/create a dataframe with just two features only “Day”,“WindSpeed” instead of all the features (which will happen if i use the way you suggested)


#4

@mohitlearns, If you are able to create a separate dataframe that satisfies the conditions, you can simple drop other features and you’ll have what you need.


#5

I agree that is one of the way to achieve the end result. But it will result is massive memory consumption ( and a time consuming process too) if the dataset is huge,

what if I just want a DataFrame with just two features based on the condition above. I am not able to achieve that result “directly” …without creating a full blown dataframe then dropping features…

.kindly let me know how to do it.

Thanks!


#6

Hi @mohitlearns,

You can use the below code to get only two columns as output:

new_df = df[(df['WindSpeed']>4) & (df['Temperature']>30)][['Day','WindSpeed']]

Hope this helps!