2

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

5
  • 1
    subset=data[(data[(['sepal length (cm)']>4.5) & (['sepal length (cm)']<6)])]. You need to wrap each condition in parentheses and create "and" logic with &. I suggest you read the documentation on boolean indexing. Commented Jan 17, 2018 at 1:53
  • 1
    Possible duplicate of Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() Commented Jan 17, 2018 at 1:59
  • I understand why the parentheses, I will look into as to why a & is required but when I try your solution I get TypeError: '>' not supported between instances of 'list' and 'float' Commented Jan 17, 2018 at 2:00
  • 1
    My fault, there was a typo in there -- you need this subset = data[(data['sepal length (cm)'] > 4.5) & (data['sepal length (cm)'] < 6)] Commented Jan 17, 2018 at 2:03
  • That worked. thank you. I realize that I did not wrap each one in parentheses and tried to add it all together. Commented Jan 17, 2018 at 2:33

1 Answer 1

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

Courtesy of @cmaher

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.