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

cmaher
  • 5,100
  • 1
  • 22
  • 34
Adam
  • 117
  • 2
  • 11
  • 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](https://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing). – cmaher Jan 17 '18 at 01:53
  • 1
    Possible duplicate of [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – cmaher Jan 17 '18 at 01: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'` – Adam Jan 17 '18 at 02: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)]` – cmaher Jan 17 '18 at 02:03
  • That worked. thank you. I realize that I did not wrap each one in parentheses and tried to add it all together. – Adam Jan 17 '18 at 02:33

1 Answers1

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

Courtesy of @cmaher

slfan
  • 8,950
  • 115
  • 65
  • 78
Adam
  • 117
  • 2
  • 11