K C
K C

Reputation: 433

Setting values in a column to 0 or 1 depending upon another column in pandas

I have a dataframe with values as follows:

  |A
 1|-0.5
 2|-0.43
 3|1
 4|3.2
 5|-0.97
 6|6.3

And I want the output to be like:

    |A     |B
   1|-0.5  |0
   2|-0.43 |0
   3|1.9   |1
   4|3.2   |1
   5|-0.97 |0
   6|6.3   |1

I tried this:

if df['A'] > 0:
   df['B'] = 1

else:
  df['B'] = 0

I got error:

ValueError                                Traceback (most recent call last)
<ipython-input-9-4dbb415bcc25> in <module>()
----> 1 if df['excess or deficit '] > 0:
      2   df['class'] = 1
      3 
      4 else:
      5   df['class'] = 0

/usr/local/lib/python3.7/dist-packages/pandas/core/generic.py in __nonzero__(self)
   1328     def __nonzero__(self):
   1329         raise ValueError(
-> 1330             f"The truth value of a {type(self).__name__} is ambiguous. "
   1331             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
   1332         )

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

Can someone please tell me how to do it?

Thanks in advance.

Upvotes: 1

Views: 1800

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195508

You can do:

df["B"] = (df["A"] > 0).astype(int)
print(df)

Prints:

      A  B
1 -0.50  0
2 -0.43  0
3  1.00  1
4  3.20  1
5 -0.97  0
6  6.30  1

Or:

df["B"] = np.where(df["A"] > 0, 1, 0)

Upvotes: 3

Related Questions