How to Transpose a Pandas DataFrame without index

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
3 min

banner

# How to Transpose a Pandas DataFrame without index

To transpose a Pandas DataFrame without the index:

  1. Use the DataFrame.set_index() method to set the index to your first DataFrame column (or your preferred column).
  2. Use the DataFrame.transpose() method to transpose the DataFrame.
main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'experience': [1, 3, 5, 7], 'salary': [175.1, 180.2, 190.3, 205.4], }) print(df) df.set_index('name', inplace=True) df = df.transpose() print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name experience salary 0 Alice 1 175.1 1 Bobby 3 180.2 2 Carl 5 190.3 3 Dan 7 205.4 -------------------------------------------------- name Alice Bobby Carl Dan experience 1.0 3.0 5.0 7.0 salary 175.1 180.2 190.3 205.4

pandas transpose dataframe without index

You can call the DataFrame.set_index() method with the first column of your DataFrame or another column name that you'd rather use as the index.

main.py
df.set_index('name', inplace=True)
We used the name column as the index, however, you can use any other column by passing its name to the set_index() method.

Notice that we also set the inplace argument to True.

When the inplace argument is set to True, the DataFrame is modified in place, rather than creating a new one.

The last step is to use the DataFrame.transpose() method.

main.py
# name Alice Bobby Carl Dan # experience 1.0 3.0 5.0 7.0 # salary 175.1 180.2 190.3 205.4 df = df.transpose()

The transpose() method returns the transposed DataFrame.

In other words, it writes the rows as columns and vice-versa.

# Transpose a Pandas DataFrame without index by using the T accessor

We used the DataFrame.transpose() method in the example above, however, you can also use the T accessor to achieve the same result.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'experience': [1, 3, 5, 7], 'salary': [175.1, 180.2, 190.3, 205.4], }) print(df) df = df.set_index('name').T print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name experience salary 0 Alice 1 175.1 1 Bobby 3 180.2 2 Carl 5 190.3 3 Dan 7 205.4 -------------------------------------------------- name Alice Bobby Carl Dan experience 1.0 3.0 5.0 7.0 salary 175.1 180.2 190.3 205.4

transpose dataframe without index using t accessor

The T property is an accessor to the transpose() method.

main.py
df = df.set_index('name').T

Notice that we didn't pass the inplace argument to set_index().

This way, the set_index method returns the DataFrame, on which we can directly access the T attribute.

The code sample above is equivalent to the following code sample.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'experience': [1, 3, 5, 7], 'salary': [175.1, 180.2, 190.3, 205.4], }) print(df) df = df.set_index('name').transpose() print('-' * 50) print(df)
The code for this article is available on GitHub

Running the Python script produces the following output.

shell
name experience salary 0 Alice 1 175.1 1 Bobby 3 180.2 2 Carl 5 190.3 3 Dan 7 205.4 -------------------------------------------------- name Alice Bobby Carl Dan experience 1.0 3.0 5.0 7.0 salary 175.1 180.2 190.3 205.4

using transpose method instead of t attribute

As shown in the code sample, using the T attribute is equivalent to calling the transpose() method on the DataFrame.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.