0

I run an 'if...else...' statement similar to:

if df['column1']==0:
    df['column2']=0
else:
    df['column2']=100*['column3']

and obtain the following "value error": The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

Since I'm fairly new to Python, I was hoping that you might guide me towards potential reasons to this error. I haven't had any luck with finding similar questions on StackOverflow. I am (unfortunately) not able to provide a data example.

Thank you.

Cecilie S. K
  • 49
  • 2
  • 8

2 Answers2

2

hope you enjoy learning python.. :)

with your coding, it means you compare all value in column1 to zero, sometimes the value is equal to zero, sometimes it is not. Those cause ambiguous.

if you want to create new column with if statement you can use apply function. it will compare value in each row.

df['column2'] = df.apply(lambda x: 0 if x['column1'] == 0 else 100*x['column3'], axis=1) 
#axis 1 means row
kevin wu
  • 171
  • 5
  • Thank you, Kevin. I do, indeed. :-) Thank you. This makes sense. If you don't mind me asking, why is it that `df['column2']=100*['column3']` (in general) is evaluated for each row? I'm essentially looking for a function that for, say, row 1 evaluates value of 'column1' in row 1 and then determines whether to set column2=0 or the function specfied above according to the value of column 1. Subsequently, the same 'procedure' is carried out for row 2 and so on.. – Cecilie S. K Mar 20 '23 at 10:38
  • in pandas, this function df['column2']=100*['column3'] calculated all row. if you want create function to evaluate each row, you can use apply like I give you example above. df.apply({your function}, axis=1). Resource : https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html – kevin wu Mar 20 '23 at 10:46
1

df['column1']==0 returns a series of booleans. So your if statement essentially equivalent to

>>> if pd.Series([True, True, False]): # I used dummy values.
...     pass

Should the if statement evaluates True because it’s not zero-length, or False because there are False values? It is unclear, so instead, pandas raises a ValueError.

Depending upon your use case, you can use any or all

>>> import pandas as pd
>>> df = pd.DataFrame({'col': [1, 2, 0]})
>>> 
>>> df
   col
0    1
1    2
2    0
>>> if (df['col'] == 0).all():
...     print("All values are zeros")
... 
>>> if (df['col'] == 0).any():
...     print("At least one of the value is zero")
... 
At least one of the value is zero

See also.

Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46