How to Transpose Pandas DataFrame

You can use the following syntax to transpose Pandas DataFrame:

df = df.transpose()

Let’s see how to apply the above syntax by reviewing 3 cases of:

  1. Transposing a DataFrame with a default index
  2. Transposing a DataFrame with a tailored index
  3. Importing a CSV file and then transposing the DataFrame

Case 1: Transpose Pandas DataFrame with a Default Index

To start with a simple example, let’s create a DataFrame with 3 columns:

import pandas as pd

data = {'A': [11,22,33],
        'B': [44,55,66],
        'C': [77,88,99]
        }

df = pd.DataFrame(data, columns = ['A', 'B', 'C'])

print (df)

Run the code in Python, and you’ll get the following DataFrame (with a default numeric index that starts from 0 as highlighted in red):

How to Transpose Pandas DataFrame

You can then add df = df.transpose() to the code in order to transpose the DataFrame:

import pandas as pd

data = {'A': [11,22,33],
        'B': [44,55,66],
        'C': [77,88,99]
        }

df = pd.DataFrame(data, columns = ['A', 'B', 'C'])

df = df.transpose()

print (df)

You’ll now get the transposed DataFrame:

How to Transpose Pandas DataFrame

Note that the columns names are now numeric starting with 0 and ending at 2. This happened because the default numeric index represented those values.

In case 2 below, you’ll see how to modify the default numeric index to your tailored index.

Case 2: Transpose Pandas DataFrame with a Tailored Index

What if you want to assign your own tailored index, and then transpose the DataFrame?

For example, let’s add the following index to the DataFrame:

index = ['X', 'Y', 'Z']

Here is the complete code that includes the tailored index:

import pandas as pd

data = {'A': [11,22,33],
        'B': [44,55,66],
        'C': [77,88,99]
        }

df = pd.DataFrame(data, columns = ['A', 'B', 'C'], index = ['X', 'Y', 'Z'])

print (df)

You’ll now see the new index on the left side of the DataFrame:

Index

Now add df = df.transpose() in order to transpose the DataFrame:

import pandas as pd

data = {'A': [11,22,33],
        'B': [44,55,66],
        'C': [77,88,99]
        }

df = pd.DataFrame(data, columns = ['A', 'B', 'C'], index = ['X', 'Y', 'Z'])

df = df.transpose()

print (df)

You’ll now get the transposed DataFrame, where the column names represent the tailored values:

Transpose Pandas DataFrame

Case 3: Import a CSV File and then Transpose the Results

At times, you may need to import a CSV file into Python, and then transpose the results.

For example, let’s say that you have the following data saved in a CSV file:

CSV File

You can then use the code below to import the data into Python (note that you’ll need to modify the path to reflect the location where the CSV file is stored on your computer):

import pandas as pd

df = pd.read_csv (r'C:\Users\Ron\Desktop\my_data.csv')

print (df)

You’ll now see the imported data:

Example of Data

Next, add df = df.transpose() to transpose the data imported (as before, you’ll need to modify the path name):

import pandas as pd

df = pd.read_csv (r'C:\Users\Ron\Desktop\my_data.csv')

df = df.transpose()

print (df)

As you can see, the column names are again numeric, starting from 0 and ending at 2 (representing the original default numeric index):

How to Transpose Pandas DataFrame

Optionally, you can rename the index values before transposing the DataFrame:

df = df.rename(index = {0:'X', 1:'Y', 2:'Z'})

Here is the complete Python code to rename the index values and then transpose the DataFrame:

import pandas as pd

df = pd.read_csv (r'C:\Users\Ron\Desktop\my_data.csv')

df = df.rename(index = {0:'X', 1:'Y', 2:'Z'})

df = df.transpose()

print (df)

And here is the new transposed DataFrame with the renamed column names:

Rename column names

You’ll get the same results by applying this code:

import pandas as pd

df = pd.read_csv (r'C:\Users\Ron\Desktop\my_data.csv')

df = df.rename(index = {0:'X', 1:'Y', 2:'Z'}).transpose()

print (df)

And here are the same results:

Transpose Pandas DataFrame

You can learn more about df.transpose() by checking the Pandas Documentation.