How to remove non-numeric characters from a column in a pandas dataframe?

Viewed 194

p_dataset

I want to remove the "Rs." and commas and just get the numeric values of the column. I tried the following code but I'm not getting the output.

def remove_charaters(value):
numbers = []
for word in value.split():
    if word.isdigit() or word=='.': #or condition for decimal point
        numbers.append(int(word))
num=''.join(numbers)
print(num)
return (float(num))

cm=[]
for i in p_dataset['City_Mileage'].astype(str):
    cm.append(remove_charaters(i))
print(cm)
p_dataset['City_Mileage']=cm
1 Answers

EDIT: nevermind the previous answer. I just saw the sample data

You can simply use

p_dataset['City_Mileage'].str.replace("Rs. ", '').astype(float)
Related