Python Forum
Transpose a dataset in pandas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Transpose a dataset in pandas
#1
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
Reply
#2
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Difficulty in understanding transpose with a tuple of axis numbers in 3-D new_to_python 0 1,554 Feb-11-2020, 06:03 AM
Last Post: new_to_python
  Transforming data with a partial transpose DivyaDS 2 2,664 Jul-19-2018, 01:34 PM
Last Post: DivyaDS

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020