Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 :

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading