mHelpMe
mHelpMe

Reputation: 6668

splitting a dataframe column into two, with one column contains numbers the other text

I have a dataframe column that contains numbers and text. I want to split the column into two columns, one column with just the numbers and the other with just text. Is this possible?

Through searching on here I found an example below, however this leaves my column unchanged.

chk = df['messy_column'].str.split('(\d+)([A-Za-z]+)', expand=True)

Example of my data,

messy_column
.965 pb ./ sm
16.3 /pb
0.33 aq.ht

expected output

 num_column      text_column
   0.965           pb ./ sm
   16.3            /pb
   0.33            aq.ht   

Upvotes: 0

Views: 47

Answers (1)

yatu
yatu

Reputation: 88226

Here's one way using str.split:

df['messy_column'].str.split('(?<=\d)(\s+).*?', expand=True).drop(1, axis=1)

    0         2
0   965  pb ./ sm
1  16.3       /pb
2  0.33     aq.ht

Upvotes: 2

Related Questions