user3116949
user3116949

Reputation: 275

Pandas df error - "The truth value of a Series is ambiguous." in if else loop

I have a pandas df with a column called group consisting of three values which are 1,2 and 3.

I am trying to do the following if else statement:

if df.group == 1:
   (code here)
elif df.group ==2:
   (code here)
else:
   (code here)

When I try to run my if else loop it is throwing the following error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Do I need to use np.where instead of the if else loop on the dataframe or if there a way to achieve this with the if else loop?

Upvotes: 1

Views: 2999

Answers (2)

Teghan Nightengale
Teghan Nightengale

Reputation: 528

In your case, df.group is a call to the series in the group column, for example:

df = pd.DataFrame({'group':[1,2,3,1,3,3,3,2,1,1]})

df.group

Out[42]: 
0    1
1    2
2    3
3    1
4    3
5    3
6    3
7    2
8    1
9    1
Name: group, dtype: int64

Thus it does not makes sense to compare a series of [1,2,3,1 ... ] to a singular value. This is what the error is trying to tell you.

It sounds like you are trying to retrieve the indexes of the column for each value in the set {1,2,3}.

In that case use:

[i for i,j in enumerate(df.group) if j == 1]

Out[48]: [0, 3, 8, 9]

Upvotes: 0

meW
meW

Reputation: 3967

You can iterate like this:

for idx, val in enumerate(df.itertuples()):
    if df.loc[idx, 'group'] == 1:
       print('1')
    elif df.loc[idx, 'group'] ==2:
       print('2')
    else:
       print('3')

Using np.where refer here

Upvotes: 3

Related Questions