One commonly used application in Pandas is the ability to flip the rows and columns of a DataFrame, allowing us to transpose it. In this tutorial, we will learn how to transpose a Pandas DataFrame using the transpose()
method and its accessor.
To demonstrate, we will be using the following Pandas DataFrame:
import pandas as pd
df = pd.DataFrame([["Aqua", "A-"],
["Ruby", "D+"],
["Barma", "C"],
["William", "B+"]],
index=[1,2,3,4],
columns=["Name", "Grade"])
Using transpose() to flip rows and columns in Pandas DataFrames:
The transpose takes one optional parameter named copy
which is False
by default. We will discuss this in more detail in the following examples.
Example 1:
Using the transpose()
method is simple enough, all you have to do is call it from the Pandas DataFrame object and it will flip the columns and rows like so:
dfT = df.transpose()
print(df)
print(dfT)
Output:
Name Grade
1 Aqua A-
2 Ruby D+
3 Barma C
4 William B+
1 2 3 4
Name Aqua Ruby Barma William
Grade A- D+ C B+
To compare both DataFrames, we printed the original DataFrame (df
) and the transposed DataFrame (dfT
).
Example 2:
However, there is one potential problem with the code written above. Any changes made to the original DataFrame are reflected in the transposed DataFrame. We will show this by changing an element in the original DataFrame after storing the transposed DataFrame in another variable.
dfT = df.transpose()
df["Name"][3] = "Gilbert"
print(df)
print(dfT)
Output:
Name Grade
1 Aqua A-
2 Ruby D+
3 Gilbert C
4 William B+
1 2 3 4
Name Aqua Ruby Gilbert William
Grade A- D+ C B+
As you can see, changing the Name
of the 3rd index
in the original DataFrame led to changes in the transposed DataFrame.
Example 3:
To counter this, we will make a Deep Copy of the original DataFrame. This is where the copy
parameter comes into play. By default it is False
, but if it is changed to True
, it creates a Deep Copy of the DataFrame it is called on.
dfT = df.transpose(copy = True)
df["Name"][3] = "Gilbert"
print(df)
print(dfT)
Output:
Name Grade
1 Aqua A-
2 Ruby D+
3 Gilbert C
4 William B+
1 2 3 4
Name Aqua Ruby Barma William
Grade A- D+ C B+
As you can see, changes made in the original DataFrame did not affect the transposed DataFrame.
Why does this happen?
Pandas makes a Deep Copy whenever the DataFrame contains elements of different data types and a Shallow Copy for DataFrames which contain elements of the same data type. Note that the index and column data types also matter.
We will now create a DataFrame consisting of only strings and transpose it, then change the value of the original DataFrame.
Example 1:
df = pd.DataFrame([["1", "432.8"],
["2", "665.1"],
["3", "756.4"],
["4", "394.3"]],
index = ["1","2","3","4"],
columns=["ID", "Pay"])
dfT = df.transpose()
df["ID"]["2"] = "5"
print(df)
print(dfT)
Output:
ID Pay
1 1 432.8
2 5 665.1
3 3 756.4
4 4 394.3
1 2 3 4
ID 1 5 3 4
Pay 432.8 665.1 756.4 394.3
As expected, the transposed DataFrame was not a Deep Copy because all the elements are of the same data type.
Example 2:
Next, we will create a DataFrame consisting of multiple data types and transpose it, then change the value of the original DataFrame.
df = pd.DataFrame([[1.0, 432],
[2.0, 665],
[3.0, 756],
[4.0, 394]],
index=["1","2","3","4"],
columns=["ID", "Pay"])
dfT = df.transpose()
df["ID"]["3"] = 8
print(df)
print(dfT)
Output:
ID Pay
1 1.0 432
2 2.0 665
3 8.0 756
4 4.0 394
1 2 3 4
ID 1.0 2.0 3.0 4.0
Pay 432.0 665.0 756.0 394.0
The transposed DataFrame did not reflect any changes made to the original DataFrame as it contained elements of different data types.
Using the T accessor:
Similar to how we used the transpose()
method, we can call the T
accessor through a Pandas DataFrame object.
Example:
dfT = df.T
print(dfT)
Output:
1 2 3 4
Name Aqua Ruby Barma William
Grade A- D+ C B+
This is identical to writing df.transpose()
, both techniques will return the same output.
This marks the end of the “How to Flip Rows and Columns in Pandas with transpose()” Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.