Pandas DataFrame mode() Method
In this tutorial, we will discuss and learn the Python pandas DataFrame.mode()
method. Using this method, we can get each element mode along the specified axis. When this method applied to the DataFrame, it returns the DataFrame which consists of the modes of each column or row.
The below is the syntax of the DataFrame.mode()
method
Syntax
DataFrame.mode(axis=0, numeric_only=False, dropna=True)
Parameters
axis: It represents index or column axis, '0' for index and '1' for the column. When the axis=0
, method applied over the index
axis and when the axis=1
method applied over the column
axis.
numeric_only: It represents the bool(True or False), the default is False. If this parameter is True
, it includes only float, int, boolean columns.
dropna: It represents the bool, and the default is True. It does not consider the null values.
Example: Find mode values of the DataFrame in Pandas
Let's create a DataFrame and get the mode value over the index
axis by assigning parameter axis=0
in the DataFrame.mode()
method. See the below example.
As we can see, the DataFrame.mode()
method returns a DataFrame that consists of the most repeated values in the DataFrame along the row
axis.
#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A":[1,2,1],"B":[2,2,1],"C":[5,2,5],"D":[4,5,4]})
print("------The DataFrame is------")
print(df)
print("---------------------------")
print(df.mode(axis=0))
------The DataFrame is------
A B C D
0 1 2 5 4
1 2 2 2 5
2 1 1 5 4
---------------------------
A B C D
0 1 2 5 4
Example 2: Find mode of the DataFrame in Pandas
Let's create a DataFrame and get the mode value over the column axis by assigning a parameter axis=1
in the DataFrame.mode()
method. See the below example.
As we can see, the DataFrame.mode()
method returns a DataFrame that consists of most repeated values in the DataFrame along the column axis.
#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A":[1,2,1],"B":[2,2,1],"C":[5,2,5],"D":[1,5,4]})
print("------The DataFrame is------")
print(df)
print("---------------------------")
print(df.mode(axis=1))
------The DataFrame is------
A B C D
0 1 2 5 1
1 2 2 2 5
2 1 1 5 4
---------------------------
0
0 1
1 2
2 1
Example 3: Find mode values of the DataFrame in Pandas
By default the DataFrame.mode()
method does not consider the null or missing values. If we set the dropna=False
in the DataFrame.mode()
method, it will consider null values also and mode can be null values. See the below example.
#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A":[1,2,None],"B":[None,2,None],"C":[5,2,5],"D":[1,5,4]})
print("------The DataFrame is------")
print(df)
print("---------------------------")
print(df.mode(axis=1,dropna=False))
------The DataFrame is------
A B C D
0 1.0 NaN 5 1
1 2.0 2.0 2 5
2 NaN NaN 5 4
---------------------------
0
0 1.0
1 2.0
2 NaN
Conclusion
In this tutorial, we learned the Python pandas DataFrame.mode()
method. We learned the syntax, parameters and applied this on the DataFrame we solved examples and understood the DataFrame.mode()
method.