Remove specific strings from dataframe

Viewed 235

I have problem to clear the string "km/kg", "kmpl", "CC", and "bhp" from these column in dataset

Here is the example dataset

Year   | Fuel_Type | Mileage    | Engine  | Power     |
2010   | LPG       | 26.6 km/kg | 998 CC  | 58.16 bhp |
2011   | Diesel    | 19.67 kmpl | 1582 CC | 126.2 bhp |

In this particular case, remove all characters from Engine, Mileage and Power columns of the dataset so that only the number remains.

2 Answers

Just use df.replace and use regex patterns for string match.

df[['Mileage','Engine','Power']] = df[['Mileage','Engine','Power']].replace(to_replace=r'([a-z/]+|[A-Z/]+)', value='', regex=True)

Prints:

  Year Fuel_Type  Mileage  Engine   Power
0  2010       LPG   26.6    998     58.16 
1  2011    Diesel  19.67    1582    126.2 

You could try to use regular expression to do this.

Here's a quick example on how you can do it. Also I assume you already know to read the dataset of yours right so what you can do is just take the column and iterate over it and apply that regex sample code I provided.

When it comes to reading data sets I personally love using pandas.

import re

l = ["26.6 km/kg","19.67 kmpl","998 CC","58.16 bhp"]


for i in l:
    t = re.sub(r'\D+$','',i)
    print(t)

Output:

26.6
19.67
998
58.16
[Finished in 0.2s]

If you're curious about \D+$ , this is what it means

\D Returns a match where the string DOES NOT contain digits "\D"

The + sign basically means any occurrences of it 1 or more.

$ means ends with

Read more about regular expressions here.

Related