Jul-05-2022, 11:29 PM
(This post was last modified: Jul-06-2022, 10:01 AM by deanhystad.)
I think you are missing that df is a dataframe. This does not work because df["State"] is a series, not an element of a list or value in a dictionary.
import pandas as pd df = pd.DataFrame({"State": ["NORMAL", "BROKEN"]}) df["label"] = 0 if df["State"] == "NORMAL" else 1
Error:Traceback (most recent call last):
File "...", line 3, in <module>
df["label"] = 0 if df["State"] == "NORMAL" else 1
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
"put your one-liner in some kind of loop" is essentially what "map()" and apply() are doing