2

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

Shankar Panda
  • 736
  • 3
  • 11
  • 27
  • 1
    What you're doing is not exactly a pivot; you are transposing the table. There are already answers for that. – jberrio Jan 11 '19 at 05:56

3 Answers3

3

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

Prayson W. Daniel
  • 14,191
  • 4
  • 51
  • 57
2

You can transpose the dataframe:

data_pivot = data_pd.T

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.transpose.html

Jetman
  • 765
  • 4
  • 14
  • 30
0

Other way is .transpose():

data_pivot = data_pd.transpose()
U13-Forward
  • 69,221
  • 14
  • 89
  • 114