0

here is a code for my data frame's grouping:

import pandas as pd

#create a list
product_list = ["bag","necklece"]
sold__commentlist = [['nice to use',"bad"],["good",'can be improved']]

#dataframe
data = pd.DataFrame (columns = ['product','sold_comment'])

#plug the list into the dataframe
data['product'] = product_list
data ['sold_comment'] = sold__commentlist

data

this is the data frame I formed

    product      sold_comment
0   bag          [nice to use, bad]
1   necklace     [good, can be improved]

Could I ask how to change the data frame into this form?

    product      sold_comment
0   bag          nice to use
0   bag          bad
1   necklace     good 
1   necklace     can be improved

Many thanks.

KiuSandy
  • 17
  • 5

1 Answers1

1

Try something like this:

import pandas as pd

#create a list
product_list = ["bag","necklece"]
sold__commentlist = [['nice to use',"bad"],["good",'can be improved']]

#dataframe
data = pd.DataFrame (columns = ['product','sold_comment'])

#plug the list into the dataframe
data['product'] = product_list
data['sold_comment'] = sold__commentlist

data = data.explode('sold_comment')

print(data)
Coderio
  • 429
  • 2
  • 9