Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract number from a string in multiple columns

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

But it’s not working.

And that’s the ouput I need:

Output

>Solution :

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())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading