4

I want to split each of these single rows in pandas dataframe into multiple rows. The need is to have the values in 's_code' and 's_percent' both split at the same time.

df

  id    s_code            s_percent   s_type   s_name   player   total-value
XI-GB-1 12220;14020;31110 35;33;32    2        yaba     p1       120398
XI-GB-2 23630;41010;23110 36;30;34    4        daba     p4       123453
XI-GB-3 15120;12511       75;25       7        doo      p3       78542
XI-GB-4 23110;23510;15110 10;55;35    9        hakuna   p2       97234
XI-GB-5 23210;23230       40;60       1        matata   p6       10000

Using some of the answers from Split (explode) pandas dataframe string entry to separate rows I was able to split single rows under one column (i.e. either s_code or s_percent) into multiple rows but that's not what I want i want the rows to be split across s_code and s_percent at the same time.

I tried splitting them separately (first by s_code and then by s_percent) and then merging them but that created multiple unnecessary mappings. The mapping I want to have, should be such that upon splitting by ';',the first value in s_code should only correspond to the first value under s_percent, and similarly the second value in s_code to second value in s_percent and so on.

The desired output that I want should be like this:

  id    s_code s_percent  s_type   s_name   player   total-value
XI-GB-1 12220  35         2        yaba     p1       120398
XI-GB-1 14020  33         2        yaba     p1       120398
XI-GB-1 31110  32         2        yaba     p1       120398
XI-GB-2 23630  36         4        daba     p4       123453
XI-GB-2 41010  30         4        daba     p4       123453
XI-GB-2 23110  34         4        daba     p4       123453
XI-GB-3 15120  75         7        doo      p3       78542
XI-GB-3 12511  25         7        doo      p3       78542
XI-GB-4 23110  10         9        hakuna   p2       97234
XI-GB-4 23510  55         9        hakuna   p2       97234
XI-GB-4 15110  35         9        hakuna   p2       97234
XI-GB-5 23210  40         1        matata   p6       10000
XI-GB-5 23230  60         1        matata   p6       10000
Aman Singh
  • 1,111
  • 3
  • 17
  • 31

0 Answers0