You can use pandas .T function to transpose a pandas dataframe using Python.

The syntax for transposing a Pandas dataframe in Python:

transposed_dataframe = original_dataframe.T

The following syntax can be demonstrated using an example in Python:

import pandas as pd

df = pd.DataFrame({'Name': ['Steve Smith', 'Joe Nadal', 'Roger Federer'], 'Age':[32, 34, 36]})

df.head()
How To Transpose A Pandas DataFrame In Python - The Click Reader

Now transposing the dataframe using the .T function,

transposed_dataframe = df.T

transposed_dataframe.head()
How To Transpose A Pandas DataFrame In Python - The Click Reader

And, that is how you transpose a pandas dataframe in Python.

In Conclusion

Do you have any problems transposing a pandas dataframe in Python? Let us know in the comment section below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here