python - 替换列中的值会清空列
问题描述
我的数据框中有一列prix
名为features
. 该列应该具有三个值:(1, 2, 3)
. 我的价值4
是噪音。我想4
用3
. 我这样做了:
# initially string and convert to int
features.prix.astype(int)
# code to replace
features['prix'] = features['prix'].replace('4', '3', inplace=True)
但是在我运行这个之后,features['prix']
变得空了。当我print(features)
,我已经None
在列Prix
。
如果我做错了什么,请告诉我。
谢谢
解决方案
当您inplace = True
在 pandas 中使用参数时,该函数会覆盖您的原始 DataFrame 并且不返回任何内容(即返回 None),因此,由于您将此结果分配给您的featrues['prix']
列,它会被 None 覆盖。
您应该选择是使用 inplace 还是将 result 分配给您的列,即:
要么做
features['prix'].replace('4','3',inplace=True)
或者
features['prix'] = features['prix'].replace('4','3')
请注意,inplace
现在不鼓励使用参数,因为他们计划最终弃用它,因此现在首选第二种方法。
推荐阅读
- sql-server - SQL Server 中的 FMTONLY 代表什么?
- r - 在 tidyr::extract 中定义正则表达式参数
- tensorflow - 运行 cloud ml 引擎时出现 errException:无法将元素作为字节获取
- java - 当我从 xml 节点获取数据时,我得到空数据
- python-3.x - Gensim doc2vec most_similar 等效于获取完整文档
- c# - 从后面的页面代码更改 UserControl 的内容
- eclipse - 在 Eclipse 上导出项目时意外覆盖了 .classpath 文件?
- vuejs2 - 如何在 Vue.js 中组合多个过滤器
- jquery - jQuery对象选择返回双组图像
- python - 摆脱 json 输出中具有空值的列