If df
looks like this:
>>> df = pd.DataFrame({'ID': [10000, 10001], 'Fruit': ['Apple, Orange, Pear', 'Apple, Banana']})
>>> print(df)
ID Fruit
0 10000 Apple, Orange, Pear
1 10001 Apple, Banana
you can use the pandas.DataFrame.apply()
method to make a new column of lists consisting of dictionaries with new rows. And after that, you can concatenate these lists in order to make a new data frame out of them. The code is following:
>>> df['new'] = df.apply(lambda row: [{'ID': row.ID, 'Fruit': item} for item in row.Fruit.split(', ')], axis=1)
>>> df_new = pd.DataFrame(df.new.sum())
>>> print(df_new)
ID Fruit
0 10000 Apple
1 10000 Orange
2 10000 Pear
3 10001 Apple
4 10001 Banana