How to Transpose Pandas DataFrame

While working with programming, there are a multitude of scenarios where a large amount of data is to be handled. Several fields like data science, machine learning and more require analysis and modification of the data, The Pandas Library provides a number of functions to perform data manipulation in an effective way. Transposing the DataFrame is one of the functions the library provides. 

What is a Pandas DataFrame?

A Pandas DataFrame is a 2D structure that can be modified. It has labeled axes. Arithmetic operations exist on both the row and column. It can be considered a dictionary with a series of objects inside it. 

Why is Transposing the Pandas DataFrame Necessary?

Transposing the Pandas DataFrame can be helpful for various reasons:

  • Helps in the adjustment of size. This is helpful for the convenient management of data.
  • Helps in aggregating the data
  • Makes the data more readable and simplified 
  • To modify the data in the columns of the DataFrame

How to Transpose Pandas DataFrame?

Taking transpose means exchanging the rows and columns.. Likewise, a Pandas DataFrame can also be transposed. The contents of the transposed object remain the same while its shape is altered. So, the transposed DataFrame will contain the same data but will just have a different arrangement.

The Pandas DataFrame can be transposed in the following ways:

  1. Using T attribute
  2. Using transpose() method
  3. Transposing without an index with transpose(copy=false) 

Let us discuss each method in detail.

Method 1: Using the T attribute

The T attribute is used to take the transpose of an array. The following code explains how a Pandas DataFrame is transposed using the T attribute.

import pandas as pd
dframe = pd.DataFrame({'COL 1': [23, 44, 25], 'COL 2': [33, 44, 54]}, index=['X', 'Y', 'Z'])
print("The data frame before the transformation is: \n ", dframe, '\n')
print("The data frame after the transformation is: \n", dframe.T)

In the above code,

  • The DataFrame is made with pd.DataFrame and saved in the variable dframe. It has two columns COL1 and COL 2 along with their respective data.
  • Inside the DataFrame, the values of the index are also defined
  • The DataFrame is printed.
  • Rows and columns of the DataFrame are swapped using the T attribute. The transposed DataFrame is then printed.

Output

The following output displays how a Pandas DataFrame can be transposed using the transpose() function:

Method 2: Using the Transpose function

Pandas DataFrame.transpose() function transposes the columns and index of the Pandas DataFrame.  It works like just how a transpose is taken in simple mathematics. The following code explains how a Pandas DataFrame can be transposed using the Transpose() function:

import pandas as pd
dframe = pd.DataFrame({'COL 1': ['hey', 22, 'Bravo'], 'COL 2': ['andrea', 13, 4]}, index=['A', 'B', 'C'])
print("The data frame before the transformation is: \n ", dframe, '\n')
print("The data frame after the transformation is: \n", dframe.transpose())

In the above code,

  • The Pandas library is imported as pd 
  • A DataFrame dframe is made with the help of pd.DataFrame. The dataframe has two columns containing string and integer values while the values of the index are provided as A, B, and C.
  • The original DataFrame is printed.
  • The transpose() function is called on dframe which exchanges its rows and columns and returns the transpose of the DataFrame. The transposed DataFrame is then printed.

Output

The following output displays how a Pandas DataFrame is transposed using the transpose() function:

Method 3: Transpose a Pandas DataFrame Without an Index

If a DataFrame needs to be transposed without an index and the index is regarded as regular data, the parameter value of copy=false must be passed to the transpose() function. The following code explains how a Pandas DataFrame can be transposed without providing the index value:

import pandas as pd
dframe = pd.DataFrame({'COL 1': [6, 1, 9], 'COL 2': [1, 3, 2], 'COL 3': [1 ,5, 60]})
print("The data frame before the transformation is: \n ", dframe, '\n')
print("The data frame after the transformation is: \n", dframe.transpose(copy=False))

In the above code,

  • The Pandas library is imported.
  • A DataFrame dframe is created using pd.DataFrame that contains two columns with integer values. The indexes are not specified. The DataFrame is printed.
  • The transpose() function is called on dframe with copy=false as its parameter.

Output

The following output displays how a Pandas DataFrame is transposed without an index in Python:

Conclusion

A Pandas DataFrame can be transposed with the T attribute or the transpose function. “copy=false” as a parameter can be passed to the transpose() function when no index is to be used. Transposing the DataFrame makes it easier to adjust the data. This makes the data more visually appealing and readable. Modifications can be made in the columns of the DataFrame enabling editing the DataFrame. This article has discussed different methods to Transpose the Pandas DataFrame and illustrates each method with an example.