Python Forum

Full Version: Transpose a dataset in pandas
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a pandas dataframe and want to transform

partyID SourceID Text 1 Name 1 Text 2 Name 2
1 1 ramesh kumar jhon pop
1 2 cherry honey juliet mariyo


transpose to dataframe

partyID SourceID Text Name
1 1 ramesh kumar
1 1 jhon pop
1 2 cherry honey
1 2 juliet mariyo

I have multiple sets with Text 1 .... 10 and Name 1 to 10) as columns in the source
In a simple way

SOURCE
df = pd.DataFrame(data = {'partyID':[100,100],
'SourceID':[100,200],
'Text 1':[2,45],
'Name 1':[23,43],
'Text 2':[3,67],
'Name 2':[32,45],
'Code Text 1':[65,98],
'Code Name 1':[90,120],
'Code Text 2':[89,65],
'Code Name 2':[10,30],

})
df.head(100)

Required
df = pd.DataFrame(data = {'partyID':[100,100,100,100],
'SourceID':[100,100,200,200],
'Text':[2,3,45,67],
'Name':[23,32,43,45],
'Code Text':[65,89,98,65],
'Code Name':[90,10,120,30]
})
df.head(100)