When I run the following code:
if df.loc[df['state_code'].isin(['12','09'])]:
It gives this error:
ValueError: The truth value of a Series is ambiguous.
Use a.empty, a.bool(), a.item(), a.any() or a.all()
What is wrong here?
The issue tells you that the Series
returned by your operation holds no intrinsic truth value. Indeed, the operation will always return something and you have to chose what you actually want:
df
which is in ['12','09']
df
are in ['12','09']
Series
contains any element, meaning not empty
So:
if not df.loc[df['state_code'].isin(['12','09'])].empty:
or
if df.loc[df['state_code'].isin(['12','09'])].any():
or
if df.loc[df['state_code'].isin(['12','09'])].all():
Check out the documentation
For more help, provide a minimal working example of your code
isin
might returnTrue
on some columns butFalse
on other columns. – MooingRawr