0

This question already has answers here: How can I pivot a dataframe? (3 answers) Your post has been associated with a similar question. If this question doesn’t resolve your question, ask a new one.

Closed 2 hours ago.

(Private feedback for you)

I have one dataframe with 4 columns i want to group dataframe by ID and do transpose of each group and at the end all groups combine in one dataframe.

here is my example df:

df=pd.DataFrame({'ID':[1,2,3,1,2,3,1,2,3],'value1':[1,33,56,7,7,8,8,67,88],'dd':[1,1,1,7,7,7,8,8,8]})


required_df=pd.DataFrame({'1':[1,7,8],'2':[33,7,67],'3':[56,8,88],'dd':[1,7,8]})

tried :

df.pivot(index='dd', columns='ID', values='value1')

result:

enter image description here


df=pd.DataFrame({'ID':[1,2,3,1,2,3,1,2,3],
                'value1':[1,33,56,7,7,8,8,67,88],
                'dd':[1,1,1,7,7,7,8,8,8]})


print (df.pivot(index='dd', columns='ID', values='value1'))
ID  1   2   3
dd           
1   1  33  56
7   7   7   8
8   8  67  88
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
waqar ali
  • 79
  • 6

0 Answers0