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.
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