0

I have a dataframe like so:

ID  | Value1  | Value2  | Tags
1   |    1    |    6    |   Alex/Jason
2   |    6    |    3    |   Miranda
3   |    5    |    9    |   Jose/Rebecca
4   |    2    |    7    |   Max
5   |    0    |    1    |   Lilly

I want to split the Tag column by a delimiter (in this case "/") and then duplicate the rows such that there is only one Tag per row:

ID  | Value1  | Value2  | Tags
1   |    1    |    6    |   Alex
1   |    1    |    6    |   Jason
2   |    6    |    3    |   Miranda
3   |    5    |    9    |   Jose
3   |    5    |    9    |   Rebecca
4   |    2    |    7    |   Max
5   |    0    |    1    |   Lilly
pault
  • 41,343
  • 15
  • 107
  • 149
NBC
  • 1,606
  • 4
  • 18
  • 31
  • Any attempt of your own? Are you using pandas? – pault Mar 07 '18 at 19:22
  • I am using pandas! I know how to split strings by delimiter, but not sure how to then replicate them into separate rows. Any tutorials you might know? – NBC Mar 07 '18 at 19:23

1 Answers1

1

There is one solution

s=df.Tags.str.split('/')

pd.concat([df[['ID','Value1','Value2']].reindex(df.index.repeat(s.str.len()))]).assign(Tags=s.sum())
Out[580]: 
   ID  Value1  Value2     Tags
0   1       1       6     Alex
0   1       1       6    Jason
1   2       6       3  Miranda
2   3       5       9     Jose
2   3       5       9  Rebecca
3   4       2       7      Max
4   5       0       1    Lilly
BENY
  • 317,841
  • 20
  • 164
  • 234