1

Currently my column is of object type and I'm trying to convert it to type numeric. But it shows the error because of special characters and string contained in it.

error:

ValueError: Unable to parse string "7`" at position 3298

code:

data['col1']=pd.to_numeric(data.col1)

So, I want to remove the special char and string from the columns that requires only number and col1 being one of it. Any suggested solution?

0

1 Answer 1

2

Using str.replace with regex pattern.

Ex:

df = pd.DataFrame({"col1": ["7`", "123", "AS123", "*&%3R4"]})
print(pd.to_numeric(df['col1'].str.replace(r"[^\d]", "")))

Output:

0      7
1    123
2    123
3     34
Name: col1, dtype: int64
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. can you elaborate on what this "r"[^\d]" does?
replace everything except numbers.

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.