'DataFrame has two features how to add a row to split them

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: enter image description here

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.



Solution 1:[1]

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')

Solution 2:[2]

Try using pandas.DataFrame.explode:

df.explode(column='feature')

Solution 3:[3]

Maybe a bit late but try this:

features = df['feature'].str.replace(r'\[.*?\]', '', regex=True) \
                        .str.get_dummies(', ')
out = pd.concat([df, features], axis=1)
print(out)

# Output
                           feature  inexpensive  picture quality  screen
0               inexpensive[+1][a]            1                0       0
1  screen[-1], picture quality[-1]            0                1       1
2                       screen[-1]            0                0       1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ynjxsjmh
Solution 2 CainĂ£ Max Couto-Silva
Solution 3 Corralien