In Polars, the Series.mode() method returns the most frequently occurring value(s) in a Series, statistically known as the mode. It is used to identify the value(s) that appear most frequently within the Series.
In this article, I will explain the syntax of the Polars Series mode() function, its parameters, and how to use it to retrieve the most frequent value(s) in a Series.
Key Points –
- The
mode()function returns the most frequently occurring value(s) in a Polars Series. - It can return one or more values if multiple values share the highest frequency.
- The method can be applied to numeric, string, and categorical data types.
- If all values occur with the same frequency,
mode()returns the entire set. - Null (
None) values are ignored during the mode calculation. mode()is a method of the Series object, not directly on DataFrame.- The result is returned as a new Series containing one or more mode values.
- If the Series has all unique values (no duplicates),
mode()returns all values.
<a id="h-polars-series-mode-introduction"></a>
Polars Series mode() Introduction
Let’s know the syntax of the series mode() function.
# Syntax of mode()
Series.mode() → Series
<a id="h-parameters-of-the-polars-series-mode"></a>
Parameters of the Polars Series mode()
A Polars Series (e.g., numbers, strings, etc.)
<a id="h-return-value"></a>
Return Value
This method returns a new Polars Series containing the most frequent value(s) in the original Series.
<a id="h-usage-of-polars-series-mode-function"></a>
Usage of Polars Series mode() Function
The mode() method in Polars identifies the value(s) that appear most frequently in a Series. It is used to determine the most common data point(s) within a column.
Now, let’s create a Polars series using a list of values and use the mode() function.
import polars as pl
ser = pl.Series("values", [10, 20, 20, 30])
print("Original Series:\n", ser)
Yields below output.
To demonstrate a numeric Polars Series with a single mode, you can apply the mode() method to a Series where one value appears more frequently than the others.
# Apply mode()
ser2 = ser.mode()
print("Mode of the Series:\n", ser2)
Here,
- The number
20appears twice, more than any other number. - Since it's the only most frequent value, it's the single mode of this Series.
<a id="h-numeric-series-with-multiple-modes"></a>
Numeric Series with Multiple Modes
You can demonstrate the use of Polars Series.mode() on a numeric Series with multiple modes, where two or more values occur with the same highest frequency.
import polars as pl
# Create a numeric Series with multiple modes
ser = pl.Series("values", [10, 20, 20, 30, 10])
# Find the mode
ser2 = ser.mode()
print("Mode of the Series:\n", ser2)
# Output:
# Mode of the Series:
# shape: (2,)
# Series: 'values' [i64]
# [
# 20
# 10
# ]
Here,
- Both
10and20appear twice. - Since they tie for the highest frequency, the
.mode()method returns both values, hence, multiple modes.
<a id="h-series-with-nulls-nulls-are-ignored"></a>
Series with Nulls (Nulls are Ignored)
When using the mode() method on a Polars Series that contains null values, Polars automatically ignores the nulls during the calculation. You can demonstrate this behavior by applying mode() to a Series with missing values.
import polars as pl
# Series with numeric values and nulls
ser = pl.Series("values", [10, 20, None, 20, None, 30])
# Find the mode
ser2 = ser.mode()
print("Mode of the Series (nulls ignored):\n", ser2)
# Output:
# Mode of the Series (nulls ignored):
# shape: (2,)
# Series: 'values' [i64]
# [
# 20
# ]
Here,
- Nulls (
None) are ignored in.mode()by default. 20appears twice, more than any other non-null value.- So, the mode is correctly computed as
20.
<a id="h-series-with-all-unique-values"></a>
Series with All Unique Values
You can demonstrate the mode() method on a Polars Series where every value is unique, that is, no duplicates exist. This shows how mode() behaves when there are no repeating values in the Series.
import polars as pl
# Create a Series where all values are unique
ser = pl.Series("values", [10, 20, 30, 40])
# Apply mode()
ser2 = ser.mode()
print("Mode of the Series (all values unique):\n", ser2)
# Output:
# Mode of the Series (all values unique):
# shape: (4,)
# Series: 'values' [i64]
# [
# 30
# 10
# 40
# 20
# ]
Here,
- Since all values occur only once, there's no single mode.
- Polars returns all values, treating them as equally frequent.
<a id="h-conclusion"></a>
Conclusion
In conclusion, the Series.mode() method in Polars is a useful tool for identifying the most frequently occurring value(s) in a Series. Whether the data contains a single mode, multiple modes, null values, or entirely unique entries, Polars handles each case gracefully.
Happy Learning!!
<a id="h-related-articles"></a>
Related Articles
- Polars Series product() Method with Examples
- Polars Series var() Usage & Examples
- Polars Series median() Function with Examples
- Polars Series std() – Explained by Examples
- Polars Series slice() – Usage & Examples
- Polars series quantile() – Usage & Examples
- Polars Series mean() – Usage & Examples
- Polars Series min() – Explained by Examples
- Polars Series interpolate() Function
- Polars rolling_mean() – Usage & Examples
- Polars Series limit() – Explained by Examples
- Polars Series fill_null() Function with Examples
<a id="h-reference"></a>
