I have a dataframe like this:
text category
sfsd sgvv abc,xyz
zydf sefs sdfsd yyy
dfsd dsrgd dggr xyz
eter vxg
Below will give the output you need. Assuming df is your dataset name.
new_df_skel = dict()
new_df_skel['text'] = list()
new_df_skel['category'] = list()
for index,item in df.iterrows():
item = dict(item)
unref_cat = item['category']
if "," in unref_cat:
for strip in unref_cat.split(','):
new_df_skel['category'].append(strip)
new_df_skel['text'].append(item['text'])
else:
new_df_skel['category'].append(strip)
new_df_skel['text'].append(unref_cat)
new_dataset = pd.DataFrame(new_df_skel)
Have a great day!