Pandas transpose()

The transpose() method in Pandas is used to interchange rows and columns of a DataFrame.

Example

import pandas as pd

# create a DataFrame
data = {
    'A': [1, 2],
    'B': [4, 5],
}

df = pd.DataFrame(data)

# transpose the DataFrame df_transposed = df.transpose()
print(df_transposed) ''' Output 0 1 A 1 2 B 4 5 '''

transpose() Syntax

The syntax of the transpose() method in Pandas is:

df.transpose(*args, copy=False)

transpose() Arguments

The transpose() method takes following arguments:

  • *args - additional arguments are for compatibility with NumPy. They are not needed for typical use cases
  • copy (optional) - determines whether a new object is created or not when transposing a DataFrame

transpose() Return Value

The transpose() method returns a new DataFrame whose rows are the columns of the original data frame and whose columns are the rows of the original DataFrame.


Example 1: Transpose a DataFrame

import pandas as pd

# create a DataFrame
data = {
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
}

# original DataFrame
original_df = pd.DataFrame(data)
print("Original DataFrame:")
print(original_df)
print("\n")

# transpose the original DataFrame transposed_df = original_df.transpose()
print("\nTransposed DataFrame:") print(transposed_df)

Output

Original DataFrame:
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9


Transposed DataFrame:
   0  1  2
A  1  2  3
B  4  5  6
C  7  8  9

Here, the original_df.transpose() method converts the columns of the original_df DataFrame into rows, and the rows into columns.


Example 2: Transpose With Mixed Data Types

import pandas as pd

# create a DataFrame with mixed data types
data = {
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['New York', 'Los Angeles', 'Chicago']
}

df = pd.DataFrame(data)

print("Original DataFrame:")
print(df)
print("\n")

# transpose the DataFrame transposed_df = df.transpose()
print("\nTransposed DataFrame:") print(transposed_df)

Output

Original DataFrame:
      Name   Age         City
0    Alice    25       New York
1      Bob    30       Los Angeles
2  Charlie   35       Chicago

Transposed DataFrame:
         0       1        2
Name     Alice   Bob      Charlie
Age      25      30        35
City  New York Los Angeles Chicago

Here, we have created the df DataFrame with mixed data types. Then we used transpose() to swap rows and column of df.


Example 3: Transpose and Change Column Headers

import pandas as pd

# create a DataFrame
data = {
    '2010': [100, 150, 200],
    '2011': [110, 160, 210],
    '2012': [120, 170, 220]
}

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

print("Original DataFrame:")
print(df)
print("\n")

# transpose the DataFrame transposed_df = df.transpose()
# rename columns after transposing transposed_df.columns = ['Sales A', 'Sales B', 'Sales C']
print("\nTransposed DataFrame with Renamed Columns:") print(transposed_df)

Output

Original DataFrame:
            2010  2011  2012
Product A   100   110   120
Product B   150   160   170
Product C   200   210   220


Transposed DataFrame with Renamed Columns:
         Sales A  Sales B  Sales C
2010      100      150      200
2011      110      160      210
2012      120      170      220

Here, to make the transposed DataFrame clearer, we have manually changed the column names from Product A, Product B, and Product C to Sales A, Sales B, and Sales C to reflect that they represent sales data for the respective products.


Example 4: Use of copy Argument in DataFrame Transposition

import pandas as pd

# define a DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
})

# display the original DataFrame
print("Original DataFrame:")
print(df)

# transpose the DataFrame with copying the data
df_transposed_copy = df.transpose(copy=True)

# display the transposed DataFrame with copy
print("\nTransposed DataFrame:")
print(df_transposed_copy)

Output

Original DataFrame:
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9
Transposed DataFrame:
   0  1  2
A  1  2  3
B  4  5  6
C  7  8  9

In the above example, The original DataFrame is displayed as is, without any modifications.

And the new DataFrame df_transposed_copy is created by transposing df and copying the data.