0

If I have a dataframe -

df = {'A':[1,2,3,4,5,6,7,8,9,10]}

How do I create a column 'B' that returns 1 if the value in column A is equal to 5 or above or 0 if the value in column A is below 5.

The Rookie
  • 877
  • 8
  • 15
  • 2
    Use `df['B'] = df['A'].ge(5).astype(int)` – Space Impact May 03 '20 at 18:49
  • 1
    Does this answer your question? [Most efficient way to convert values of column in Pandas DataFrame](https://stackoverflow.com/questions/35639588/most-efficient-way-to-convert-values-of-column-in-pandas-dataframe) – Ben.T May 03 '20 at 18:51

2 Answers2

0

Use the list comprehension together with if statement, just as below:

df['B'] = [1 if x >= 5 else 0 for x in df.A]
Henrique Branco
  • 1,778
  • 1
  • 13
  • 40
0

Try:

import numpy as np
df['B']=np.where(df['A'].lt(5), 0, df['A'])
Grzegorz Skibinski
  • 12,624
  • 2
  • 11
  • 34