search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas DataFrame | mode method

schedule Aug 11, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas DataFrame.mode(~) method computes the mode of each column or row of the DataFrame. If the counts of some values are the same, then all their modes will be returned.

Parameters

1. axislink | int or string | optional

Whether to compute the mode row-wise or column-wise:

Axis

Description

"index" or 0

Mode is computed for each column.

"columns" or 1

Mode is computed for each row.

By default, axis=0.

2. numeric_onlylink | None or boolean | optional

The allowed values are as follows:

Value

Description

True

Only numeric rows/columns will be considered (e.g. float, int, boolean).

False

Attempt computation with all types (e.g. strings and dates), and throw an error whenever the mode cannot be computed.

By default, numeric_only=False.

3. dropnalink | boolean | optional

Whether or not to ignore NaN. By default, dropna=True.

Return Value

A DataFrame holding the mode of each row or column of the source DataFrame.

Examples

Consider the following DataFrame:

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

Column-wise mode

To compute the mode column-wise:

df.mode() # or axis=0
A B C
0 3.0 3 2.0
1 NaN 4 NaN
2 NaN 5 NaN

Here, note the following:

  • the mode of the first column is 3.

  • the mode of the second column is 3, 4 and 5 since each of these values occur exactly once.

  • since mode(~) returns all the modes, we end up with 3 rows here, and NaN is used to fill the empty entries.

  • the return type is DataFrame.

Row-wise mode

To compute the mode row-wise, set axis=1:

df.mode(axis=1)
0 1 2
0 2.0 NaN NaN
1 2.0 3.0 4.0
2 2.0 3.0 5.0

Specifying numeric_only

Consider the following DataFrame:

df = pd.DataFrame({"A":[4,4], "B":[2,True]})
df
A B
0 4 2
1 4 True

Here, column B contains mixed types.

False

By default, numeric_only=False, which means that rows/columns with mixed types will also be considered:

df.mode() # numeric_only=False
A B
0 4.0 True
1 NaN 2

True

By setting numeric_only=True, only numeric rows/columns will be considered:

df.mode(numeric_only=True)
A
0 4

Notice how column B was ignored since it contained mixed types.

Specifying dropna

Consider the following DataFrame with some missing values:

df = pd.DataFrame({"A":[3,3,pd.np.nan],"B":[5,pd.np.nan,pd.np.nan]})
df
A B
0 3.0 5.0
1 3.0 NaN
2 NaN NaN

By default, dropna=True, which means that all NaN are ignored:

df.mode() # dropna=True
A B
0 3.0 5.0

To consider missing values:

df.mode(dropna=False)
A B
0 3.0 NaN

Here, note the following:

  • even though column A contained a NaN, the right mode (3) is returned.

  • the mode for column B is NaN since it occurred twice, whereas the value 5 only occurred once.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!