Last updated: Apr 12, 2024
Reading time·3 min
To transpose a Pandas DataFrame
without the index:
DataFrame.set_index()
method to set the index to your first
DataFrame
column (or your preferred column).DataFrame.transpose()
method to transpose the DataFrame
.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)
Running the code sample produces the following output.
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
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.
df.set_index('name', inplace=True)
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.
# 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.
T
accessorWe used the DataFrame.transpose()
method in the example above, however, you
can also use the T
accessor to achieve the same result.
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)
Running the code sample produces the following output.
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
The T
property is an accessor to the transpose()
method.
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.
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)
Running the Python script produces the following output.
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
As shown in the code sample, using the T
attribute is equivalent to calling
the transpose()
method on the DataFrame
.
You can learn more about the related topics by checking out the following tutorials: