Home > OS >  creating new column based on condition (python)
creating new column based on condition (python)

Time:02-11

I have a dataframe where one of the columns, "deals" is a TRUE/FALSE boolean. I want to create a new column that populates 1 when the "deals" column is True, and 0 when the "deals" columns is False.

I tried the following code but it's giving me all zeros. Please help

df['madedeal'] = np.where(df['deal']=='True', 1, 0)

CodePudding user response:

You can simply use astype(int) which converts True to 1 and False to 0 (no need for np.where here):

df['madedeal'] = df['deal'].astype(int)

CodePudding user response:

You have to compare with True, not "True" because "True" is str.

Also, it would be more recommended to use is when you compare with True or False rather than == (PEP 8: E712 comparison to True should be 'if cond is True:' or 'if cond:').

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'deal': [True, False, True, False]
})

df['madedeal'] = np.where(df['deal'] is True, 1, 0)
# df['madedeal'] = np.where(df['deal'], 1, 0 # This also works as @sammywemmy commented.

print(df)

#    deal  madedeal
#0   True         1
#1  False         0
#2   True         1
#3  False         0
  • Related