Based on multiple conditions how to get a sub-group of DataFrame in Python?

Viewed 108

Below I have a DataFrame and I want to get simply a sub-category of dataframe , which consist of only those records for which column("day") will be ' friday ' and for non-smokers only . BAsically I want to calculate a boxplot for non-smoker's total_bill at friday . But I need to get a sample from whole df , as I have to calculate it for other also and maybe even more conditions to specify.

df.head()

     total_bill     tip   sex    smoker  day    time    size
 0   16.99          1.01  Female    No  Sun    Dinner   2
 1   10.34          1.66    Male    No  Sun    Dinner   3
 2   21.01          3.50    Male    No  Sun    Dinner   3
 3   23.68          3.31    Male    No  Sun    Dinner   2
 4   24.59          3.61  Female    No  Sun    Dinner   4
1 Answers

You can use .loc to access the dataframe with your two conditions, and add additional conditions as needed.

df[(df.day =="Fri") & (df.smoker == "No")]
Related