0

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
MarkS
  • 1,455
  • 2
  • 21
  • 36
  • There is no filtration before your code? If yes, need `copy` – jezrael Apr 19 '20 at 13:51
  • 1
    `df['Churn']==1` should be fine but the current `df` may have been referenced from another df. How did you create the `df`? if you have performed any filtering from another dataframe to create the current `df` , use a `.copy()` there – anky Apr 19 '20 at 13:51
  • 1
    ```@anky```, That was it. In my code, I filtered out some of the columns one line before. Much obliged. – MarkS Apr 19 '20 at 13:58

0 Answers0