Transpose the dataframe in pandas Python

T is the function used to transpose the dataframe in pandas  python. Let’s see how to

  • Transpose the data from rows to columns and from columns to rows in pandas python

Let’s first create the dataframe.

import pandas as pd
import numpy as np

#Create a DataFrame
df1 = {
    'Name':['George','Andrea','micheal','maggie','Ravi','Xien','Jalpa'],
    'State':['Arizona','Georgia','Newyork','Indiana','Florida','California','Texas'],
    'Gender':["M","F","M","F","M","M","F"],      
    'Score':[63,48,56,75,32,77,85],
    
   }


df1 = pd.DataFrame(df1,columns=['Name','State','Gender','Score'])
print(df1)

So the resultant dataframe will be

Transpose the dataframe in pandas - Python 1

 

Transpose simply means to change the rows to columns and columns to rows. This can be accomplished by T function as shown below


transposed_df =df1.T
print(transposed_df)

So the transposed dataframe will be

transposed_df:

Transpose the dataframe in pandas - Python 2

 

Transpose the dataframe in pandas Python                                                                                                                Transpose the dataframe in pandas Python