For my class I need to find the sepal length in the iris dataset that is between
ID: 3875936 • Letter: F
Question
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
Explanation / Answer
Hi,
The error The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). is a pretty common one,
what is happening here is
subset=data[data[(data['petal width (cm)']>4.5 and data['petal width (cm)']<6)]]
in the above line, you are using 'AND' that means python expects logical truth values but in pandas they are ambigous, so you should be using bitwise operator like &
so, now re write the expression to
subset=data[data[(data['petal width (cm)']>4.5 & data['petal width (cm)']<6)]]
Thumbs up if this was helpful, otherwise let me know in comments
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.