Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replicating the DataFrame row in a special manner

I want to replicate data frame rows by splitting the contact number, I'm trying several ways but unable to do so. Please help

Input:

df

col1        mob_no             col3
 a    9382949201/3245622535    45
 b    8383459345/4325562678    67
 c    8976247543/1827472398    89
 d    7844329432               09

Expected output:

col1    mob_no      col3
 a    9382949201     45
 a    3245622535     45
 b    8383459345     67
 b    4325562678     67
 c    8976247543     89
 c    1827472398     89
 d    7844329432     09
like image 293
genz_on_code Avatar asked Dec 23 '22 15:12

genz_on_code


1 Answers

Try with str.split + DataFrame.explode:

df['mob_no'] = df['mob_no'].str.split('/')
df = df.explode('mob_no')
  col1      mob_no  col3
0    a  9382949201    45
0    a  3245622535    45
1    b  8383459345    67
1    b  4325562678    67
2    c  8976247543    89
2    c  1827472398    89
3    d  7844329432    09
like image 52
Henry Ecker Avatar answered Dec 28 '22 11:12

Henry Ecker