The pandas **transpose** function allows us to transpose a dataframe. Transposing a dataframe reflects the rows into columns and columns into rows over the main diagonal.

`transposed_df = df.transpose()`

We can also use the pandas T function to transpose a dataframe.

`transposed_df = df.T`

When working with data as a data science or data analyst, manipulating the structure of our datasets can be very useful for the efficient processing of data.

We can use the pandas **tranpose** function to transpose dataframes. Transposing a dataframe involves reflecting the rows into columns over the main diagonal, and reflecting the columns into rows over the main diagonal.

Let’s say I have the following dataframe.

```
Name Weight Change
0 Jim -16.20
1 Sally 12.81
2 Bob -20.45
3 Sue 15.35
4 Jill -12.43
5 Larry -18.52
```

We can use the pandas **transpose** function to transpose this dataframe in the following way.

```
print(df.tranpose())
#Output:
0 1 2 3 4 5
Name Jim Sally Bob Sue Jill Larry
Weight Change -16.2 12.81 -20.45 15.35 -12.43 -18.52
```

You can also see here that the transpose of a transposed dataframe is the original dataframe.

```
print(df.tranpose().tranpose())
#Output:
Name Weight Change
0 Jim -16.20
1 Sally 12.81
2 Bob -20.45
3 Sue 15.35
4 Jill -12.43
5 Larry -18.52
```

## Is there a difference between the pandas transpose and pandas T functions?

There is no difference between the pandas **transpose** and pandas **T** functions. The pandas **T** function is uses the pandas **transpose** function directly.

Let’s say we have the same dataframe as above, and let’s call both the pandas **tranpose** function and pandas **T** function to transpose the dataframe.

```
transposed_with_transpose = df.transpose()
transposed_with_T = df.T
print(transposed_with_transpose)
print(transposed_with_T)
#Output:
0 1 2 3 4 5
Name Jim Sally Bob Sue Jill Larry
Weight Change -16.2 12.81 -20.45 15.35 -12.43 -18.52
0 1 2 3 4 5
Name Jim Sally Bob Sue Jill Larry
Weight Change -16.2 12.81 -20.45 15.35 -12.43 -18.52
```

As you can see above, the results are the same. The pandas **tranpose** function and the pandas **T** function produce the same results.

Hopefully this article has been helpful for you in your understanding of the pandas **transpose** function and how to transpose dataframes in pandas.

## Leave a Reply