Python Forum
Transpose a dataset in pandas - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Transpose a dataset in pandas (/thread-30326.html)



Transpose a dataset in pandas - simhadriraju - Oct-16-2020

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


RE: Transpose a dataset in pandas - simhadriraju - Oct-16-2020

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)