1

I am trying to extract a dataframe from the built-in seaborn dataset diamond that satisfies two conditions :

  • i) The cut is 'ideal'
  • ii) The color is 'E'.

The code snippet is below:

import pandas as pd
import seaborn as sns
diam = sns.load_dataset("diamonds")

cut_col = diam[(diam['cut'] == "Ideal") and (diam['color'] == "E"]

This gives me the following ValueError:

File "C:\Users\logic\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1555, in __nonzero__
    self.__class__.__name__

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

But if I replace the logical operator 'and' with the bitwise operator '&', the code runs fine. Can anyone explain the reason behind this? Why the logical-AND operator does not work in this case?

1 Answer 1

1

It's not the same as you would do in if statement, so use:

cut_col = diam[(diam['cut'] == "Ideal")&(diam['color'] == "E")]

For more info check out boolean indexing.

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.