API · Pandas

Pandas DataFrame DataFrame.transpose() Function

Transpose a pandas DataFrame by exchanging its rows and columns, then inspect the resulting values and dtypes.

On this page

Use DataFrame.transpose() to exchange a pandas DataFrame’s rows and columns. The returned DataFrame uses the original row labels as column labels and the original column labels as row labels.

Syntax of DataFrame.transpose()

Call the method on the object you want to transpose: df.transpose(). It returns a transposed DataFrame; the original object remains available as df.

This guide focuses on the no-argument call and leaves version-specific options out of scope.

Example: transpose a DataFrame with DataFrame.transpose()

This example creates a small table with student names, attendance, and marks. After the call, each original column becomes a row, and each original index label becomes a column.

import pandas as pd

dataframe = pd.DataFrame(
    {
        "Attendance": [60, 100, 80, 78, 95],
        "Name": ["Olivia", "John", "Laura", "Ben", "Kevin"],
        "Obtained Marks": [90, 75, 82, 64, 45],
    }
)

transposed = dataframe.transpose()
print(transposed)

The labels make it easier to check the direction of the exchange: Attendance, Name, and Obtained Marks label rows in the result, while 0 through 4 label its columns.

Transpose a DataFrame with homogeneous data types

When every column contains integers, the input has a homogeneous value type. Transpose it and inspect both sets of column dtypes to see how this example is represented in your pandas environment.

import pandas as pd

dataframe = pd.DataFrame(
    {
        "A": [6, 20, 80, 78, 95],
        "B": [60, 50, 7, 67, 54],
    }
)

transposed = dataframe.transpose()
print("Original values:")
print(dataframe)
print("Transposed values:")
print(transposed)
print("Original dtypes:")
print(dataframe.dtypes)
print("Transposed dtypes:")
print(transposed.dtypes)

This input has five rows, labeled 0 through 4, and two columns, A and B. The result therefore has two rows and five columns. Check the displayed dtype information for this specific input rather than assuming that every numeric DataFrame will have the same dtype after transposition; dtype behavior can depend on the values and dtype representation in use.

Transpose a DataFrame with mixed data types

A DataFrame can have columns with different types. Here, two columns contain numbers and one contains names. The code shows the values and dtypes before and after the operation so you can inspect the result for this input.

import pandas as pd

dataframe = pd.DataFrame(
    {
        "Attendance": [60, 100, 80, 78, 95],
        "Name": ["Olivia", "John", "Laura", "Ben", "Kevin"],
        "Obtained Marks": [90, 75, 82, 64, 45],
    }
)

transposed = dataframe.transpose()
print("Original values:")
print(dataframe)
print("Transposed values:")
print(transposed)
print("Original dtypes:")
print(dataframe.dtypes)
print("Transposed dtypes:")
print(transposed.dtypes)

Each transposed row combines values that came from one original column. Since those rows include both strings and numbers, inspect the resulting dtypes instead of treating the mixed example as a rule for every pandas version or dtype system. The method changes the axes; it does not make distinct source columns homogeneous before transposing.