为了账号安全,请及时绑定邮箱和手机立即绑定

Python如何在应该只是数字的列中替换非数字值

Python如何在应该只是数字的列中替换非数字值

慕码人2483693 2022-06-07 19:56:00
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   1436791982795    8/10/2019   site2796    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= 0for 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   2834940052793    8/10/2019   2834940052794    8/10/2019   1436791982795    8/10/2019   2834940072796    8/10/2019   283494007
查看完整描述

1 回答

?
达令说

TA贡献1821条经验 获得超6个赞

你可以试试:

cw["campaignid"]=cw["campaignid"].replace({'brand':283494005,'site':283494007}).astype(int)



查看完整回答
反对 回复 2022-06-07
  • 1 回答
  • 0 关注
  • 179 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号