Home > Software engineering >  DataFrame has two features how to add a row to split them
DataFrame has two features how to add a row to split them

Time:04-23

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.

CodePudding user response:

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

CodePudding user response:

Try using pandas.DataFrame.explode:

df.explode(column='feature')

CodePudding user response:

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
  • Related