0

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

the goal is to find all the non numeric values in the column ["campaignid"] and attribute a numeric value for them.

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

My attempts


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

Desired result

oNLY NUMERIC VALUES

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


2
  • 3
    cw["campaignid"]=cw["campaignid"].replace({'brand':283494005,'site':283494007}).astype(int) ? Commented Sep 26, 2019 at 18:17
  • Works!! thank you :) Commented Sep 26, 2019 at 18:21

1 Answer 1

1

you can try:

cw["campaignid"]=cw["campaignid"].replace({'brand':283494005,'site':283494007}).astype(int)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.