Pandas DataFrame | mode method
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. axis
link | int
or string
| optional
Whether to compute the mode row-wise or column-wise:
Axis | Description |
---|---|
| Mode is computed for each column. |
| Mode is computed for each row. |
By default, axis=0
.
2. numeric_only
link | None
or boolean
| optional
The allowed values are as follows:
Value | Description |
---|---|
| Only numeric rows/columns will be considered (e.g. |
| 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. dropna
link | 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 C0 2 3 21 3 4 22 3 5 2
Column-wise mode
To compute the mode column-wise:
df.mode() # or axis=0
A B C0 3.0 3 2.01 NaN 4 NaN2 NaN 5 NaN
Here, note the following:
the mode of the first column is
3
.the mode of the second column is
3
,4
and5
since each of these values occur exactly once.since
mode(~)
returns all the modes, we end up with 3 rows here, andNaN
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 20 2.0 NaN NaN1 2.0 3.0 4.02 2.0 3.0 5.0
Specifying numeric_only
Consider the following DataFrame:
df = pd.DataFrame({"A":[4,4], "B":[2,True]})df
A B0 4 21 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 B0 4.0 True1 NaN 2
True
By setting numeric_only=True
, only numeric rows/columns will be considered:
df.mode(numeric_only=True)
A0 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 B0 3.0 5.01 3.0 NaN2 NaN NaN
By default, dropna=True
, which means that all NaN
are ignored:
df.mode() # dropna=True
A B0 3.0 5.0
To consider missing values:
df.mode(dropna=False)
A B0 3.0 NaN
Here, note the following:
even though column
A
contained aNaN
, the right mode (3
) is returned.the mode for column
B
isNaN
since it occurred twice, whereas the value5
only occurred once.