How to Use Mode in pandas: Find Most Frequent Values Easily
Use the
mode() method in pandas to find the most frequent value(s) in a Series or DataFrame column. It returns the mode(s) as a Series, handling multiple modes if they exist.Syntax
The mode() method can be used on a pandas Series or DataFrame. It returns the most frequent value(s) in the data.
Series.mode(dropna=True): Returns the mode(s) of the Series.dropna=Trueignores missing values.DataFrame.mode(axis=0, numeric_only=False, dropna=True): Returns mode(s) for each column (axis=0) or row (axis=1).
python
series.mode(dropna=True) dataframe.mode(axis=0, numeric_only=False, dropna=True)
Example
This example shows how to find the mode of a pandas Series and a DataFrame column. It demonstrates handling multiple modes and ignoring missing values.
python
import pandas as pd # Create a Series with multiple modes data = pd.Series([1, 2, 2, 3, 3, 4, None]) # Find mode of the Series mode_values = data.mode() # Create a DataFrame df = pd.DataFrame({ 'A': [1, 2, 2, 3, 3], 'B': ['x', 'y', 'y', 'z', 'z'] }) # Find mode of each column mode_df = df.mode() print('Mode of Series:') print(mode_values) print('\nMode of DataFrame columns:') print(mode_df)
Output
Mode of Series:
0 2.0
1 3.0
dtype: float64
Mode of DataFrame columns:
A B
0 2 y
1 3 z
Common Pitfalls
Common mistakes when using mode() include:
- Expecting a single value when multiple modes exist.
mode()returns all modes as a Series or DataFrame. - Not handling missing values properly. By default,
dropna=Trueignores NaNs, but setting it toFalseincludes NaNs as possible modes. - Confusing
mode()withvalue_counts().mode()returns the most frequent value(s), whilevalue_counts()returns counts for all values.
python
import pandas as pd # Wrong: expecting single value s = pd.Series([1, 1, 2, 2, 3]) mode_wrong = s.mode()[0] # This works but may miss other modes # Right: handle multiple modes mode_all = s.mode() print('Single mode (may miss others):', mode_wrong) print('All modes:', mode_all)
Output
Single mode (may miss others): 1
All modes: 0 1
1 2
dtype: int64
Quick Reference
| Method | Description | Default Parameters |
|---|---|---|
| Series.mode() | Returns mode(s) of a Series | dropna=True |
| DataFrame.mode() | Returns mode(s) for each column or row | axis=0, numeric_only=False, dropna=True |
| dropna parameter | Whether to ignore NaN values | True |
| Returns | Series or DataFrame with mode values | N/A |
Key Takeaways
Use pandas
mode() to find the most frequent value(s) in data.mode() returns all modes, not just one, so expect multiple values if tied.Missing values are ignored by default but can be included by setting
dropna=False.For DataFrames,
mode() works column-wise by default and returns a DataFrame.Don't confuse
mode() with value_counts(); they serve different purposes.