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

separating text and numbers in pandas Python

how can I separate a text from a number in Panda Python, if it’s not already separated by a space (" ")?

In my data frame df I have a column with 0.3314QSF.

I want to split this into two columns containing respectively 0.3314 and QSF in two different columns within my data frame.
The data frame consists of thousands of rows, this rule should be working for all the rows.

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

Thank you

>Solution :

You can do it use str.extract:

>>> df['a'].str.extract(r'([\d.]+)(\w+)')
        0    1
0  0.3314  QSF

Or using str.split (you’ll have an extra empty entry but you can remove):

>>> df['a'].str.split(r'(=?[\d.]+)', expand=True)
  0       1    2
0    0.3314  QSF

>>> df['a'].str.split(r'(=?[\d.]+)', expand=True).drop(0, axis=1)
        1    2
0  0.3314  QSF
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