Python Pandas

Pandas Dataframe Transpose

The “Pandas” library is an open-source library in Python that we use to clean or analyze data. There are multiple Pandas methods that are used to achieve particular tasks on data. While operating with DataFrame, sometimes we require to swap the rows and columns (Transpose). The “DataFrame.transpose()” method is used in Python to transpose the DataFrames.

This write-up explains how to determine the transpose of DataFrame using the below contents:

What is the “DataFrame.transpose()” Method in Python?

In Python, the “DataFrame.transpose()” method is utilized to transpose the DataFrame index and columns. This method flips the DataFrame across its main diagonal by switching columns and rows. We can access or call this method using the “T” attribute or the “transpose()” method.

Syntax

DataFrame.transpose(*args, copy=False)

 

Parameters

In this syntax, the “*args” are optional extra keywords that have no impact but might be taken for compatibility with NumPy. The “copy” parameter is an optional parameter that indicates whether to copy DataFrame data after transposing.

Return Value

The “DataFrame. transpose()” method retrieves the transpose DataFrame.

Example 1: Finding the Transpose of the Input DataFrame

Here in this example, we first create the DataFrame by using the “pandas.DataFrame()” method. Next, we use the “df.transpose()” method to determine the transpose of the DataFrame:

import pandas
df = pandas.DataFrame({'Name':['Anna', 'Joseph', 'Lily', 'Sam', 'Henry'],
                       'Age':[24, 15, 52, 18, 11]}, index=['A', 'B', 'C', 'D', 'E'])
print(df, '\n')
result = df.transpose()
print(result)

 

The below output shows the transpose of the DataFrame:

Example 2: Finding the Transpose of the Input DataFrame With “T” Attribute

We can also find the transpose of the input DataFrame with the “df.T”  attribute. Here is the code that will uses this attribute to find transpose:

import pandas
df = pandas.DataFrame({'Name':['Anna', 'Joseph', 'Lily', 'Sam', 'Henry'],
                       'Age':[24, 15, 52, 18, 11]}, index=['A', 'B', 'C', 'D', 'E'])
print(df, '\n')
print(df.T)

 

According to the following output, we have successfully found the transpose of the DataFrame:

Example 3: Finding the Transpose of the Input DataFrame and Checking the Columns Data Types

This example code determines the data types of DataFrame columns before and after finding transpose. The “df.dtypes” retrieve the DataFrame column types and the “df.transpose()” determines the DataFrame transpose:

import pandas
df = pandas.DataFrame({'Name':['Anna', 'Joseph', 'Lily', 'Sam', 'Henry'],
                       'Age':[24, 15, 52, 18, 11]}, index=['A', 'B', 'C', 'D', 'E'])
print(df, '\n')
print(df.dtypes, '\n')
result = df.transpose()
print(result, '\n')
print(result.dtypes)

 

The DataFrame column types along with the DataFrame transpose have been retrieved successfully:

Example 4: Finding the Transpose of the Input DataFrame Without the Index Column

In this example code, we determine the transpose of the DataFrame without any index column. First, we create the DataFrame with the default index. After that, the “df.set_index()” is used to set the “Name” column as an index. Lastly, we apply the “transpose()” method on the DataFrame to retrieve the transpose:

import pandas
df = pandas.DataFrame({'Name':['Anna', 'Joseph', 'Lily', 'Sam', 'Henry'],
                       'Age':[24, 15, 52, 18, 11]})
print(df, '\n')
result = df.set_index('Name').transpose()
print(result)

 

The provided output retrieved the transpose of the DataFrame without any index column:

Conclusion

The “DataFrame.transpose()” method or the “DataFrame.T” attribute is utilized to transpose the DataFrame index and columns. We can use this method to find the transpose of DataFrame by flipping rows and columns. This blog showed multiple examples of DataFrame transpose via the “df.transpose()” method.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.