While working over dataframes, we might be interested to take transpose for various mathematical or visualization porpose.
So this recipe is a short example on How to transpose a dataframe in Pandas. Let's get started.
import pandas as pd import seaborn as sb
Let's pause and look at these imports. Pandas is generally used for performing mathematical operation and preferably over arrays. Seaborn will help us in importing dataset.
df = sb.load_dataset('tips') print(df.head())
Here we have imported datset from seaborn library.
Now, our dataset is ready.
tdf = df.T print(tdf.head())
Pandas has function "T" which can easily help us transpose.
Once we run the above code snippet, we will see:
Scroll down the ipython file to visualize the final output.
We can see a new dataframe being created as transpose of our original dataframe.