My best efforts to convert a column with ‘yes’ ‘no’ values to True, False or 1 , 0 are failing. The column is ‘subscribed’.
df.subscribed.unique() returns array(['no', 'yes'], dtype=object)
Tried the following. None of them worked:
df.subscribed = df.subscribed.astype(int) df.subscribed.map(dict(yes=1, no=0)) df.replace({'subscribed': {'yes': 1, 'no': 0}}) d = {'yes': True, 'no': False} df['subscribed'].map(d)
Advertisement
Answer
As EdChum points out you need to assign back to the df.
df = pd.DataFrame({'subscribed':np.random.choice(['yes','no'], 10)}) print(df)
Input:
subscribed 0 yes 1 yes 2 yes 3 no 4 no 5 yes 6 no 7 no 8 no 9 yes df =df.replace({'subscribed': {'yes': True, 'no': False}}) print(df)
Output:
subscribed 0 True 1 True 2 True 3 False 4 False 5 True 6 False 7 False 8 False 9 True