python-3.x - np.where 的语法替换列值
问题描述
我能够使用 np.where() 成功地将 Yes\No 值替换为 1\0。
但是,无论我使用什么语法,我总是会收到两个 SettingWithCopyWarning 警告之一。我想知道正确的语法,这样我就可以避免警告。
我尝试了三种不同的方法。他们都工作,但他们都产生警告:
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
解决方案
推荐阅读
- c - VLA 原型和多维数组参数
- javascript - 带有 lit-element 的 Google Maps JavaScript API
- c++ - 以数组形式访问所有对象元素
- javascript - 我有适用于 1 个视频的代码。如何使其适用于多个视频?
- android - 如何在片段内的对象中实现 onDragListener?
- angular6 - Angular 6 HttpClient 未将响应转换为接口
- bitbucket - 在 Bitbucket 上的 README.md 中写入一个数字,但没有指向提交的链接
- django - RelatedObjectDoesNotExist 在 /login/
- bash - 为 3 个不同的变量创建一个循环以输出所有可能的组合
- scala - 如何从 Either 返回 Right 类?