0

I am completely new to programming and started learning Python recently.

I have a pandas data frame df as shown in image 1 and trying to rearrange the columns as shown in image 2.

Can you please help me to complete this.

From

To

Thanks and Regards, Arya.

mosc9575
  • 5,618
  • 2
  • 9
  • 32
Arya
  • 57
  • 3
  • Welcome to the forum! Please check out [this post](https://stackoverflow.com/help/how-to-ask) to learn about asking answerable questions. Please also avoid posting code in the form of images, as that makes it difficult to extract data to play with. – warped Feb 15 '21 at 07:02

1 Answers1

1

You can use pd.pivot_table like this:

df=pd.DataFrame({'index':[0,1,2,0,1,2],'Name':['A','A','A','B','B','B'],'Value':[10,20,30,15,25,35]})
df.pivot_table(index='index',columns='Name',values='Value').reset_index()
Out[8]: 
Name  index   A   B
0         0  10  15
1         1  20  25
2         2  30  35
Suhas Mucherla
  • 1,383
  • 1
  • 5
  • 17