• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:15 mins read
You are currently viewing How to Transpose DataFrame in Pandas?

Pandas transpose() function is used to transpose rows(indices) into columns and columns into rows in a given DataFrame. It returns transposed DataFrame by converting rows of the original DataFrame as columns and vice versa.

In this article, I will explain the concept of Pandas transpose() function and using this syntax and parameters how we can transpose the given DataFrame in several ways.

1. Quick Examples of Transpose DataFrame

If you are in hurry, below are some quick examples of how to transpose DataFrame.


# Quick examples of transpose dataframe 

# Example 1: Transpose the rows as columns
transposed_df = df.transpose()

# Example 2: Transpose single column of DataFrame
technologies= {'Fee' :[22000,25000,23000,24000,26000]}
df = pd.DataFrame(technologies)
transposed_df = df.transpose()

# Example 3: Transpose DataFrame without index
transposed_df = df.set_index('Courses').transpose()

# Example 4: Transpose the DataFrame
transposed_df = df.transpose()
# Check the dtype of transposed DataFrame
print(transposed_df.dtypes)

2. Syntax of transpose()

Below is the syntax of Pandas transpose()


# Syntax of transpose()
DataFrame.transpose(*args, copy=False)

2.1 Parameters of transpose()

Following are the parameters of transpose() function.

  • copy:This is a boolean parameter, which is optional. If copy is set to True, it returns a copy of the transposed DataFrame. If set to False (default), it returns a view on the original DataFrame.
  • *args, **kwargs: This allows you to pass specific axes to transpose. For example, you can pass axes=(1, 0) to swap both rows and columns. By default, it transposes the DataFrame along all axes.

2.2 Return Value

It returns transposed DataFrame.

3. Pandas transpose() Usage

Pandas transpose() function is used to interchange the axes of a DataFrame, in other words converting columns to rows and rows to columns. In some situations we want to interchange the data in a DataFrame based on axes, In that situation, Pandas library provides transpose() function. Transpose means the process of exchanging axes labels of DataFrame.

Let’s create a Pandas DataFrame with a Python dictionary of lists, pandas DataFrame columns names CoursesFeeDurationDiscount.


# Create DataFrame
import pandas as pd
import numpy as np
technologies= {
    'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
    'Fee' :[22000,25000,23000,24000,26000],
    'Duration':['30days','50days','35days', '40days','35days'],
    'Discount':[1000,2300,1000,1200,2500]
          }
df = pd.DataFrame(technologies, index = ['a', 'b', 'c', 'd', 'e'] )
print("Create DataFrame:\n",df)

Yields below output.

Pandas DataFrame transpose

4. Transpose DataFrame rows to Columns

Apply Pandas transpose() function over the dataframe then, this syntax will interchange rows as columns and columns as rows and it returns transposed DataFrame, where the rows are columns of the original DataFrame and columns, are rows of the original DataFrame.


# Transpose the rows as columns
transposed_df = df.transpose()
print("Transposed DataFrame:\n",transposed_df)

Yields below output.

Pandas DataFrame transpose

If the original DataFrame has mixed dtypes then, we will get a object dtype of transposed DataFrame.


# Check the dtype of DataFrame
print(df.dtypes)

# Output:
# Courses     object
# Fee          int64
# Duration    object
# Discount     int64
# dtype: object

# Check the dtype of transposed DataFrame
print(transposed_df.dtypes)

# Output:
# a    object
# b    object
# c    object
# d    object
# e    object
# dtype: object

When we have a homogeneous dtype of the original DataFrame then, we will get a transposed DataFrame with the same dtype. Let’s check whether our DataFrame can contain homogeneous dtype or not. If it contains the same dtype, its transposed dtype will also contain the same dtype.

Let’s Create homogeneous dtype of DataFrame, then apply transpose() function.


# Create homogeneous dtype of DataFrame
technologies= {'Fee' :[22000,25000,23000,24000,26000],
               'Discount':[1000,2300,1000,1200,2500]
          }
df = pd.DataFrame(technologies)
# Check the dtype of DataFrame
print(df.dtypes)

# Output:
# Fee         int64
# Discount    int64
# dtype: object

# Transpose the DataFrame
transposed_df = df.transpose()
# Check the dtype of transposed DataFrame
print(transposed_df.dtypes)

# Output:
# 0    int64
# 1    int64
# 2    int64
# 3    int64
# 4    int64
# dtype: object

5. Transpose the Specified Column of Pandas

So far, we have learned how to transpose the whole Dataframe using the transpose() function. In this example, we will learn how to transpose specified column of a given DataFrame using this function. Let’s see how it transpose,


# Transpose single column of DataFrame
technologies= {'Fee' :[22000,25000,23000,24000,26000]}
df = pd.DataFrame(technologies)
print(df)
print("#DataFrame After Transpose...")
transposed_df = df.transpose()
print(transposed_df)

Yields below output.


# Output:
      Fee
0  22000
1  25000
2  23000
3  24000
4  26000

# DataFrame After Transpose...
         0      1      2      3      4
Fee  22000  25000  23000  24000  26000

6. Transpose DataFrame without index

Using the transpose() function with the help of set_index() we can transpose a given DataFrame without an index. Below syntax will return the transposed DataFrame with no index. Let’s apply the above function on DataFrame.


# Transpose DataFrame without index
transposed_df = df.set_index('Courses').transpose()
print(transposed_df)

Yields below output.


# Output:
Courses    Spark PySpark  Hadoop  Python  Pandas
Fee        22000   25000   23000   24000   26000
Duration  30days  50days  35days  40days  35days
Discount    1000    2300    1000    1200    2500

Frequently Asked Questions on How to Transpose() DataFrame in Pandas

What does the transpose() function do in Pandas?

The transpose() function in Pandas is a method that is used to interchange rows and columns in a DataFrame. It effectively flips the DataFrame along its main diagonal, swapping rows and columns. In other words, the rows become columns, and the columns become rows.

How do I use the transpose() function on a DataFrame?

To use the transpose() function on a DataFrame in Pandas, you can call the method on the DataFrame object. Additionally, you can use the .T attribute as a shorthand for transposing.

Is there an alternative method to transpose a DataFrame?

Besides using the transpose() method, you can also use the .T attribute to achieve the same result. Both methods are equivalent and can be used interchangeably. Here’s an example using the .T attribute.

Does the transpose() function modify the original DataFrame?

The transpose() function does not modify the original DataFrame. Instead, it returns a new transposed DataFrame. If you want to modify the original DataFrame, you need to assign the result back to the original DataFrame or use the .T attribute in place.

Can I transpose only specific rows or columns in a DataFrame?

You can transpose specific rows or columns in a DataFrame by selecting those rows or columns first and then applying the transpose() function.

Are there any performance considerations when using transpose() on large DataFrames?

While the transpose() function is generally efficient, transposing large DataFrames may have performance implications. It’s recommended to be mindful of memory usage and processing time, especially when working with extensive datasets.

Conclusion

In this article, I have explained the concept of Pandas transpose() function and using this syntax and parameters how we can transpose the given DataFrame in several ways with examples.

Happy learning!!

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium