Extract number from a string in multiple columns

Question:

I have several columns that I want to extract the numbers from a string. I have this example:

My input

I’ve tried this code to extract the number for both columns:

df1 = df['Diatom col', 'Cistos col'].str.extract('(d+)')

But it’s not working.

And that’s the ouput I need:

Output

Asked By: Susana Martins

||

Answers:

here is one way to do it

If you post a data as a text or code, I’ll be able to share the result.

Assumption: all numbers are together and not interspersed with non-digits, with exception of , and .

# replace out all characters that are not digits or comma or period.


(df[['Type','Size']]
 .apply(lambda x: x.str.replace(r'[^d.,]','', regex=True) , axis=1)
 .reset_index())
Answered By: Naveed
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.