Transpose Pandas DataFrame

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)

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 yellow):

    A   B   C
0  11  44  77
1  22  55  88
2  33  66  99

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)

df = df.transpose()

print(df)

You’ll now get the transposed DataFrame:

    0   1   2
A  11  22  33
B  44  55  66
C  77  88  99

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, index=['X', 'Y', 'Z'])

print(df)

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

    A   B   C
X  11  44  77
Y  22  55  88
Z  33  66  99

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, index=['X', 'Y', 'Z'])

df = df.transpose()

print(df)

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

    X   Y   Z
A  11  22  33
B  44  55  66
C  77  88  99

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:

ABC
114477
225588
336699

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:

    A   B   C
0  11  44  77
1  22  55  88
2  33  66  99

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):

    0   1   2
A  11  22  33
B  44  55  66
C  77  88  99

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:

    X   Y   Z
A  11  22  33
B  44  55  66
C  77  88  99

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:

    X   Y   Z
A  11  22  33
B  44  55  66
C  77  88  99

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

Leave a Comment