Function to replicate rows of dataframe if column contains list

Apply the explode method, for example

df_exploded = (
        df.set_index(["Date", "Item", "Count"])
        .apply(pd.Series.explode)
        .reset_index()
    )

will result in

df_exploded
>>>
    Date        Item         Count  Tags
0   2020-10-28  My_fake_item    3   A
1   2020-10-28  My_fake_item    3   B

and there’s no need to check if an element is a list or not on the column

import pandas as pd 

# create dummy dataframe
df = {'Date': ['2020-10-28', '2020-11-01'],
      'Item': ['My_fake_item', 'My_other_item'],
      'Tags': [['A', 'B'], 'C'],
      'Count': [3, 5]}

df = pd.DataFrame(df, columns=['Date', 'Item', 'Tags', 'Count'])

will result in

          Date  Item          Count Tags
0   2020-10-28  My_fake_item    3   A
1   2020-10-28  My_fake_item    3   B
2   2020-11-01  My_other_item   5   C

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top