python - Python如何在应该只是数字的列中替换非数字值
问题描述
Python如何在应该只是数字的列中替换非数字值
目标是在 ["campaignid"] 列中找到所有非数值,并为它们分配一个数值。
dataframe:
2790 8/31/2019 143679198
2791 8/10/2019 143679198
2792 8/10/2019 brand
2793 8/10/2019 brand
2794 8/10/2019 143679198
2795 8/10/2019 site
2796 8/10/2019 site
我的尝试
cw= cw["campaignid"].replace(cw["campaignid"]({'brand': "283494005"}), inplace=True)
#error: NoneType' object is not subscriptable
# Tried with for loop as well, but isnt replacing it.
count= 0
for x in cw.campaignid:
if x == "brand":
x = "283494005"
print(f"brand now is.. {x}")
elif x == "site":
x = "283494007"
print(f"site now is.. {x}")
else:
count+= 1
期望的结果
只有数字值
2790 8/31/2019 143679198
2791 8/10/2019 143679198
2792 8/10/2019 283494005
2793 8/10/2019 283494005
2794 8/10/2019 143679198
2795 8/10/2019 283494007
2796 8/10/2019 283494007
解决方案
你可以试试:
cw["campaignid"]=cw["campaignid"].replace({'brand':283494005,'site':283494007}).astype(int)
推荐阅读
- node.js - 重新加载页面时如何保存和加载画布?(反应js)
- typescript - 默认情况下,如何在 TypeScript 中通过索引访问数组?
- r - 在 R 中排列列
- r - 使用R存储在列中的文本中某个单词的出现
- javascript - 无法在 Javascript 中使用 ajax 加载 .php 文件
- authentication - 每个会话必须执行哪些用于翻译的 gcloud 命令?
- c++ - 声明和初始化 C++ 静态变量
- html - 是否有任何 google chrome 扩展程序(或任何其他浏览器扩展程序)可以将滚动网页并排转换为多张页面?
- javascript - chrome 扩展的“让我登录”
- javascript - PDF.js 未以正确的页面顺序显示 PDF 文件