Transposing
Transposing swaps rows and columns, like rotating the table a quarter turn.
In this page:
Syntax
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
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
- Losing dtypes after transposing mixed data
- Forgetting the index becomes columns
- 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: