1

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?

user3116949
  • 265
  • 1
  • 5
  • 14
  • possible to add a sample v/s expected data? – anky Feb 21 '19 at 14:52
  • 1
    See https://stackoverflow.com/questions/18194404/create-column-with-elif-in-pandas. You can use `np.where` or `np.select`, which would be preferred over `apply(axis=1)` Or perhaps also `.map` – ALollz Feb 21 '19 at 14:53
  • 2
    You can use something like [`np.select`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.select.html) depending on what the "some code" is. You'd generally want to avoid looping. – roganjosh Feb 21 '19 at 14:58

2 Answers2

3

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

meW
  • 3,832
  • 7
  • 27
0

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]