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

Split the entries in dataframe

here is my sample data:

df=pd.DataFrame({'Name':['A,B','C','D','E,F,G']
              ,'Age':[4,6,8,9]})

My expected output is to split the entries if there are more than one names.

pd.DataFrame({'Name':['A','B','C','D','E','F','G']
              ,'Age':[4,4,6,8,9,9,9]})

I can only split the name but I don’t now how to make it duplicated entries.
For first row, there are A,B under the Name, so I want to make it into two separate rows, A and B both are aged 4. Similarly, E,F,G are all aged at 9, so I want to convert this row into three rows with same age at 9.

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

df['Name'].apply(lambda x : x.split(','))

>Solution :

Combine df.set_index + str.split + explode:

df.set_index('Age')['Name'].str.split(',').explode().reset_index()

   Age Name
0    4    A
1    4    B
2    6    C
3    8    D
4    9    E
5    9    F
6    9    G
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