0

I have a pandas dataframe in the following format:

      id                                           category
0  17074820                      [15153999, 15213210, 7668302]
1  15153999   [12721363, 9096352, 10788337, 9114021, 10330360]
2  15213210                               [11466240, 12184798]
3   7668302                                          [1539589]
4  12721363  [9465087, 11842208, 11309498, 9465125, 9990074...

I want to add id values corresponding to list in rows, eg:

      id                         category
0  17074820                      15153999
0  17074820                      15213210
0  17074820                      7668302
jpp
  • 159,742
  • 34
  • 281
  • 339
CyberPunk
  • 1,297
  • 5
  • 18
  • 35

1 Answers1

2

This is one solution via numpy and itertools:

import pandas as pd, numpy as np
from itertools import chain

df = pd.DataFrame([[17074820, [15153999, 15213210, 7668302]],
                   [15153999, [12721363, 9096352, 10788337, 9114021, 10330360]]],
                  columns=['id', 'category'])

lens = list(map(len, df['category']))

res = pd.DataFrame({'id': np.repeat(df['id'], lens),
                    'category': list(chain.from_iterable(df['category']))})

print(res)

   category        id
0  15153999  17074820
0  15213210  17074820
0   7668302  17074820
1  12721363  15153999
1   9096352  15153999
1  10788337  15153999
1   9114021  15153999
1  10330360  15153999
jpp
  • 159,742
  • 34
  • 281
  • 339