← Back to Pandas Course | Chapter 9: Reshaping Data | Lesson 5 of 6

Transposing

Transposing swaps rows and columns, like rotating the table a quarter turn.

In this page:

  1. Transposing
Syntax
python
transposed = df.T

Transposing

df.T flips rows and columns, so the index becomes the column headers. It is useful for viewing wide summaries such as describe() output. Mixed dtypes become object after transposing.

Note: df.describe().T is often easier to read.

Example: Transposing

python
import pandas as pd

df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}, index=["r1", "r2"])
print(df.T)
print(df.describe().T[["mean", "max"]])

# Output:
#    r1  r2
# a   1   2
# b   3   4
#    mean  max
# a   1.5  2.0
# b   3.5  4.0
Related Topics
Common Mistakes
  1. Losing dtypes after transposing mixed data
  2. Forgetting the index becomes columns
  3. Transposing very large tables
Chapter Summary
  • .T swaps rows and columns
  • Index becomes column headers
  • Mixed dtypes become object
  • describe().T is a handy view
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.