How to pivot a pandas Dataframe in Python?

Viewed 656

I am trying to pivot the below dataframe. I want the column names to be added as rows. First row is a statis one but the Column names are not static since they will be calculated for the all numerical columns from the data frame. Could you please help.

This is my data frame:

enter image description here

Expected Dataframe:

enter image description here

3 Answers

You just add .T :) df.describe().T to transpose your results:

import pandas as pd
import numpy as np

#Create a Dictionary of series
d = {'Name':pd.Series(['Alisa','Bobby','Cathrine','Madonna','Rocky','Sebastian','Jaqluine',
   'Rahul','David','Andrew','Ajay','Teresa']),
   'Age':pd.Series([26,27,25,24,31,27,25,33,42,32,51,47]),
   'Score':pd.Series([89,87,67,55,47,72,76,79,44,92,99,69])}

#Create a DataFrame
pd.DataFrame(d).describe().T

Results: enter image description here

Other way is .transpose():

data_pivot = data_pd.transpose()
Related