Home > other >  How to divide a string in parts to use in a dataframe
How to divide a string in parts to use in a dataframe

Time:10-07

I have a column in a dataframe like this:

   description
link Miami portforward 155 gb
Link Berlin main  10 mbps
link Mexico sub-rout 1 mb

I want as output:

description                value      unit
link Miami portforward      155        gb
Link Berlin main            10         mbps
link Mexico sub-rout         1         mb

i tried something like [x for x in column if x isdigit()] but this is a complete fail.

How can I solve this?

CodePudding user response:

using rsplit and limit the split to 2

df['description'].str.rsplit(' ', 2,expand=True)
    0   1   2
1   link Miami portforward  155     gb
2   Link Berlin main    10  mbps
3   link Mexico sub-rout    1   mb

OR

alternately, using regex

df[['col1','col2']]=df['description'].str.extract(r'(\d )\s(.*)')
df
    description     col1    col2
1   link Miami portforward 155 gb   155     gb
2   Link Berlin main 10 mbps    10  mbps
3   link Mexico sub-rout 1 mb   1   mb
  • Related