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.
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.
Use the list comprehension together with if statement, just as below:
df['B'] = [1 if x >= 5 else 0 for x in df.A]
Try:
import numpy as np
df['B']=np.where(df['A'].lt(5), 0, df['A'])