Splitting the row into new line and adding corresponding matching value from other column

Viewed 1223

I have a datafarme like following

  data = pd.DataFrame({'Name': ['CTA15;CTA16;CAR;', 'AC007;AC008;GOO7;G008;F009', 'AC09;BC09;C09;V09;B0P', 'UF09;GF09;HF09;MN08'],  
    'Sample':['JAK_1', 'TOR2', 'Gilo', 'ALR']})
data

                Name    Sample
0   CTA15;CTA16;CAR;           JAK_1
1   AC007;AC008;GOO7;G008;F009  TOR2
2   AC09;BC09;C09;V09;B0P       Gilo
3   UF09;GF09;HF09;MN08         ALR

And I need to split the column Name and into a new row and add the value in column Sample correspondingly.In the end, I am aiming to have a data frame like this,

    Name    Sample
0   CTA15   JAK_1
1   CTA16   JAK_1
2   CAR JAK_1
3   AC007   TOR2
4   AC008   TOR2
5   GOO7    TOR2
6   G008    TOR2
7   F009    TOR2
8   AC09    Gilo
9   BC09    Gilo
10  C09 Gilo
11  V09 Gilo
12  B0P Gilo
13  UF09    ALR
14  GF09    ALR
15  HF09    ALR
16  MN08    ALR

I need to split with ';' into the new line and add the value from Sample column to each correspondingly.

1 Answers
Related