Pandas DataFrame Mode Method

PythonPythonBeginner
Practice Now

Introduction

This lab will guide you through the usage of the mode() method in Pandas DataFrame. The mode() method is used to find the most frequently occurring value(s) in a DataFrame.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/booleans -.-> lab-68670{{"`Pandas DataFrame Mode Method`"}} python/lists -.-> lab-68670{{"`Pandas DataFrame Mode Method`"}} python/tuples -.-> lab-68670{{"`Pandas DataFrame Mode Method`"}} python/dictionaries -.-> lab-68670{{"`Pandas DataFrame Mode Method`"}} python/importing_modules -.-> lab-68670{{"`Pandas DataFrame Mode Method`"}} python/numerical_computing -.-> lab-68670{{"`Pandas DataFrame Mode Method`"}} python/data_analysis -.-> lab-68670{{"`Pandas DataFrame Mode Method`"}} python/build_in_functions -.-> lab-68670{{"`Pandas DataFrame Mode Method`"}} end

Import the necessary libraries

First, import the Pandas library using the import statement:

import pandas as pd

Create a DataFrame

Now, create a DataFrame using the DataFrame() constructor. For this example, let's create a DataFrame with three columns: "A", "B", and "C".

df = pd.DataFrame({"A": [1, 2, 1], "B": [2, 2, 1], "C": [5, 2, 5]})

Find the mode value(s)

To find the mode value(s) of each column in the DataFrame, use the mode() method:

df_mode = df.mode()
print(df_mode)

The mode() method returns a DataFrame containing the mode value(s) of each column.

Specify the axis

By default, the mode() method finds the mode value(s) along the column axis (axis=0). If you want to find the mode value(s) along the row axis, specify axis=1.

For example, to find the mode value(s) along the row axis, use the following code:

df_mode = df.mode(axis=1)
print(df_mode)

Include only numeric columns

If you want to include only numeric columns in the mode calculation, you can use the numeric_only parameter. By default, numeric_only is set to False. Set it to True to include only numeric columns.

For example, to include only numeric columns in the mode calculation, use the following code:

df_mode = df.mode(numeric_only=True)
print(df_mode)

Include null values

By default, the mode() method does not consider null values. If you want to include null values in the mode calculation, you can use the dropna parameter. By default, dropna is set to True. Set it to False to include null values.

For example, to include null values in the mode calculation, use the following code:

df_mode = df.mode(dropna=False)
print(df_mode)

Summary

In this lab, you learned how to use the mode() method in Pandas DataFrame to find the most frequently occurring value(s). You also learned how to specify the axis, include only numeric columns, and include null values. Now you can use this knowledge to find the mode value(s) in your own data analysis projects.

Other Python Tutorials you may like