I have a DataFrame that contains a column called feature
that can have more than one of them as illustrated in the image below row 3 & 4
. How do a add a row to the DataFrame that splits the two features:
so for row 3
as an example having:
sentiment = neg feature = screen[-1], picture quality[-1] attribute = category = screen sentence = when the screen was n't contracting or glitch...
and row 4
:
sentiment = neg feature = screen[-1], picture quality[-1] attribute = category = picture quality sentence = when the screen was n't contracting or glitch...
so the idea is to add a row with the same information except for the category that now contains the second feature. The features can be up to 10.
Thank you in advance, would truly appreciated assistance on this.
Advertisement
Answer
You can try split the column value by ,
then explode on feature
column.
df['feature'] = df['feature'].str.split(', ') # If there is not always a space after comma, use `apply` #df['feature'] = df['feature'].apply(lambda feature: list(map(str.strip, feature.split(',')))) df = df.explode('feature')