繁体   English   中英

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

[英]Python How replace non numeric values in a column when it should be only numeric

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM