I am able to successfully replace Yes\No values with 1\0 using np.where().
However, no matter what syntax I use, I always get one of two SettingWithCopyWarning warnings. I would like to know the correct syntax so I can avoid the warning.
I have tried three different ways. They all work, but they all generate the warning:
df['Churn'] = np.where(df['Churn'].values == 'Yes', 1, 0)
df['Churn'] = np.where(df.loc[:, 'Churn'].values == 'Yes', 1, 0)
C:/Users/Mark/PycharmProjects/main/main.py:62: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
df['Churn'] = np.where(df['Churn'].values == 'Yes', 1, 0)
df.loc[:, 'Churn'] = np.where(df.loc[:, 'Churn'].values == 'Yes', 1, 0)
C:\Users\Mark\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexing.py:966: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
self.obj[item] = s