0

I have this piece of code:

watches_df = input_csv[input_csv["Name-of-CSV-column"] == "Value-A"].copy()

No, I need to rewrite it so instead of selecting only Value-A, I need also to work with the values Value-B and Value-C from this column. I tried different ways how to write it down, but I get the error below.

Basically, I would need something like:

watches_df = input_csv[input_csv["Name-of-CSV-column"] == "Value-A" OR "Value-B" OR "Value-C"].copy()

but obviously this is wrong as I get this error:

File "/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py", line 1479, in nonzero
f"The truth value of a {type(self).name} is ambiguous.

How can I fix it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

1 Answers1

1

This is a Pandas-specific question, not Python per se.

You seem to want pandas.Series.isin if your data is in a dataframe:

values = {"Value-A", "Value-B", "Value-C"}
watches_df = input_csv[input_csv["Name-of-CSV-column"].isin(values)].copy()

With a raw Python list-of-dicts, you'd do something like

watches = [watch for watch in watches if watch["column"] in values]
AKX
  • 152,115
  • 15
  • 115
  • 172