Adam
Adam

Reputation: 117

Filtering DataFrame on multiple conditions in Pandas

For my class I need to find the sepal length in the iris dataset that is between 4.5 and 6. exact specifications are "Select the rows that that has sepal length greater than 4.5 but less than 6" the given code (which the answer should be in this form) is

    subset=data[data['petal width (cm)']>2]

what I have is:

    subset=data[data[(['sepal length (cm)']>4.5 and ['sepal length (cm)']<6)]]

the main problem I am having is getting the syntax correct. in its current state, I get an error stating

    TypeError: '>' not supported between instances of 'list' and 'float'

If it is changed to

    subset=data[data[(data['petal width (cm)']>4.5 and data['petal width (cm)']<6)]]

I get the error

    ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

thank you

Upvotes: 2

Views: 7830

Answers (1)

Adam
Adam

Reputation: 117

subset = data[(data['sepal length (cm)'] > 4.5) & (data['sepal length (cm)'] < 6)]

Courtesy of @cmaher

Upvotes: 2

Related Questions