Search code examples
pythonpandasdataframemultiple-columns

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.

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