4

I have a normal df

 A = pd.DataFrame([[1, 5, 2], [2, 4, 4], [3, 3, 1], [4, 2, 2], [5, 1, 4]],
                  columns=['A', 'B', 'C'], index=[1, 2, 3, 4, 5])

If I want to create a column based on a condition in another column I do something like this, and works as expected.

 In [5]: A['D'] = A['C'] > 2
 In [6]: A
 Out[6]: 
   A  B  C      D
1  1  5  2  False
2  2  4  4   True
3  3  3  1  False
4  4  2  2  False
5  5  1  4   True

However, If I want to do the same using 2 conditions...like:

A['D'] = A['C'] > 2 and A['B'] > 2      or     A['D'] = A['C'] > 2 & A['B'] > 2

I get the infamous

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

How can I solve without iteration? The purpose of creating this new column based on two conditions is to be able to use a groupby function of the type:

A.groupby('D').apply(custom_fuction)

So, perhaps there's a way to use groupby to do the whole thing, but I don't know how to do it.

Thanks

hernanavella
  • 5,462
  • 8
  • 47
  • 84

1 Answers1

1

Use &, not and, to perform an elementwise logical-and operation:

In [40]: A['D'] = (A['C'] > 2) & (A['B'] > 2)

In [41]: A
Out[41]: 
   A  B  C      D
1  1  5  2  False
2  2  4  4   True
3  3  3  1  False
4  4  2  2  False
5  5  1  4  False

You also could skip defining the D column:

In [42]: A.groupby((A['C'] > 2) & (A['B'] > 2))
Out[42]: <pandas.core.groupby.DataFrameGroupBy object at 0xab5b6ac>
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677