-1

I have a dataframe with a 'power' cars column of object type. I need to get the numeric values of ch. The data is like:

df = pd.DataFrame({'power': ['265 kW (360 ch)', '125 à 135 kW (170 à 184 ch)', '244 kW (332 ch) en 75D,']})

That's what I need:

df = pd.DataFrame({'power': [360, 184, 332]})

Can a power Regex person help me please? Or maybe with another technical expertise without being Regex?

Note: it's not just a question of how get a simple number in a string, please Mister Wiktor Stribizew, stoping mark people question as duplicated! Sorry if you have arrived to later to answer it! Thanks

1 Answers1

1

You can use df.apply with re.search:

>>> df.power.apply(lambda x: re.search(r'(\d+) ch', x).group(1))
0    360
1    184
2    332
a_guest
  • 34,165
  • 12
  • 64
  • 118