I have a pandas dataframe
in which one column (titled 'labels') contains a string of comma-separated values within a list:
name title labels
John III ['ABC, DEF, GHI']
I would like to create new rows that separates the list at each comma, and duplicate the data for all the other columns (name, title, etc):
name title labels
John III ['ABC']
John III ['DEF']
John III ['GHI']
This answer using explode should work, but pandas is interpreting my column as an object. I tried to convert the column to a string: df['labels'].astype(str)
but that didn't work, and this df['labels'].apply(', '.join)
separates at the letter level rather than the whole word.
Any thoughts?