How to transpose a Pandas dataframe using T and transpose()

Learn how to transpose data and flip the orientation of a Pandas dataframe using the T and transpose() functions.

How to transpose a Pandas dataframe using T and transpose()
Picture by Pixabay, Pexels.
5 minutes to read

When working with Pandas dataframes that contain many columns, or those containing very large amounts of content, it is often useful to display the dataframe by flipping its orientation through a process called transposing.

Transposing a dataframe flips the orientation of the columns and rows so the column headers become row indexes and the row index values become column headers. It can make wide data much easier to read and avoids the need to scroll sideways.

In this quick tutorial I’ll show you how you can use the Pandas pandas.DataFrame.T and pandas.DataFrame.transpose() functions to transpose data and flip your dataframes on their sides to give you a better view of the values.

Create a Pandas dataframe

To get started, open a Jupyter notebook and either import data into a Pandas dataframe or create a dataframe from scratch.

import pandas as pd

df = pd.DataFrame(
    [('Pterophyllum altum', 'Pterophyllum', 12.5), 
     ('Coptodon snyderae', 'Coptodon', 8.2),
     ('Astronotus ocellatus', 'Astronotus', 31.2), 
     ('Corydoras aeneus', 'Corydoras', 5.3),
     ('Xenomystus nigri', 'Xenomystus', 5.3)
    ], 
    columns=['species', 'genus', 'length_cm']
)
df
species genus length_cm
0 Pterophyllum altum Pterophyllum 12.5
1 Coptodon snyderae Coptodon 8.2
2 Astronotus ocellatus Astronotus 31.2
3 Corydoras aeneus Corydoras 5.3
4 Xenomystus nigri Xenomystus 5.3

Transpose the data orientation with .T

First, we’ll use the most common method for transposing data - the pandas.DataFrame.T function. This takes a dataframe as its input and returns a transformed and transposed dataframe. It won’t modify the original, so if you want to save your data in its transposed form you’ll need to overwrite your original dataframe object.

After running the T function, the species, genus, and length_cm column headers have now become index values and the row values have become column header names.

df.T
0 1 2 3 4
species Pterophyllum altum Coptodon snyderae Astronotus ocellatus Corydoras aeneus Xenomystus nigri
genus Pterophyllum Coptodon Astronotus Corydoras Xenomystus
length_cm 12.5 8.2 31.2 5.3 5.3

Using head() with T

Since your actual dataframe might be very large, unlike the simple dummy dataframe we created above, you’ll probably only want to view some of the data. You can do this using the Pandas head() method. For example, to show the first row from our original dataframe but in its transposed orientation we call df.head(1) first and then append .T.

df.head(1).T
0
species Pterophyllum altum
genus Pterophyllum
length_cm 12.5

Transpose the dataframe orientation with transpose()

Pandas includes a second function for transposing dataframes called pandas.DataFrame.transpose(). I’m not entirely sure why two functions are required. The T method works fine in most cases and is shorter to write than transpose(), but transpose() does take an additional copy argument that lets you define whether a copy is made or not.

To transpose our data using transpose() we simply append it to the dataframe object as df.transpose(). As with T, the transpose() method takes a dataframe as its input and returns a dataframe as its output, so you need to save it back to the original object to keep the transposed formatting.

df.transpose()
0 1 2 3 4
species Pterophyllum altum Coptodon snyderae Astronotus ocellatus Corydoras aeneus Xenomystus nigri
genus Pterophyllum Coptodon Astronotus Corydoras Xenomystus
length_cm 12.5 8.2 31.2 5.3 5.3

Using transpose() with head()

Just as with the T function, if you only want to view some of the rows from your original dataframe in a transposed orientation you need to call df.head() and define the number of rows as an integer argument and then append transpose() using dot notation.

df.head(3).transpose()
0 1 2
species Pterophyllum altum Coptodon snyderae Astronotus ocellatus
genus Pterophyllum Coptodon Astronotus
length_cm 12.5 8.2 31.2

Matt Clarke, Sunday, January 01, 2023

Matt Clarke Matt is an Ecommerce and Marketing Director who uses data science to help in his work. Matt has a Master's degree in Internet Retailing (plus two other Master's degrees in different fields) and specialises in the technical side of ecommerce and marketing.