1
I am creating a chart with dataset tips, for this I need to filter the data based on two or more conditions. My goal is to count the number of men, and the number of women. And also count only the number of men smoking and the number of women smoking. The code below does this but adding more conditions gives error. How I can do this filtering using two more conditions?
import pandas as pd
import seaborn as sns
df = sns.load_dataset("tips")
men = df[df.sex=='Male'].count()[0]
women = df[df.sex=='Female'].count()[0]
# isso dar erro
men_smoker = df[df.sex=='Male' & df.smoker=='Yes'].count()[0]
That is the mistake TypeError: unsupported operand type(s) for &: 'str' and 'Categorical'
Dataframe:
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
It’s python use boolean operator
and
in place of the bit-to-bit operator&
. See the operator table– Augusto Vasques
@Augustovasques already tried it doesn’t work either
– Bernardo Lopes
Each condition has to be between parentheses. Example
df[(df[col] == "coisa") & (df[col1] > 10)]
. In your casemen_smoker = df[(df.sex=='Male') & (df.smoker=='Yes')].count()[0]
. When you only have one condition, the parenthesis is not necessary. My suggestion, always use it.– Paulo Marques