In Pandas, the `mode()`

function is used to find the mode(s) of the values in a Series. The mode is the value(s) that appears most frequently in the Series. This function returns a Series containing the mode(s). If there are multiple modes, all will be included in the returned Series.

In this article, I will explain the `series.mode()`

function and using its syntax, parameters, and usage how we can return the mode(s) of the underlying data in the given Series object. This function always returns a Series object, even if only one value is returned as the mode. If there are multiple modes with the same frequency, all of them are included in the resulting Series.

**Key Points –**

- The
`mode()`

function returns the value(s) that appear most frequently in the Series. - It returns multiple modes if multiple values have the same highest frequency.
- The function has an optional
`dropna()`

parameter that defaults to`True`

, which excludes`NaN`

values from the calculation. - When
`dropna`

is set to`False`

,`NaN`

values are included in the computation of the mode. - The return value is a Series containing the mode(s), with the data type matching the original Series.

## Syntax of Pandas Series mode()

Following is the syntax of the series mode() function.

```
# Syntax of series mode()
Series.mode(dropna=True)
```

### Parameters of the Series mode()

Following are the parameters of the mode() function.

`dropna`

– This is a boolean parameter (default is`True`

). If set to`True`

, it excludes missing values (NaN) from the computation. If set to`False`

, it includes missing values in the computation.

### Return Value

A Pandas Series containing the mode(s) and their frequencies. If there are multiple modes with the same frequency, all of them are returned. If the Series is empty, a Series with a single NaN value is returned.

## Finding the Mode of a Series of Integers

Finding the mode of a series of integers using the `mode()`

function in Pandas.

To run some examples of the pandas series mode() function, let’s create a Pandas Series from a Python list.

```
import pandas as pd
# Creating a Pandas Series of integers
series = pd.Series([5, 10, 10, 15, 20, 20, 20, 25])
print("Original Series:\n",series)
```

Output.

The `mode()`

function in Pandas is used to find the mode(s) of a Series. Mode is the most frequently occurring value(s) in the Series. To find the mode of a series of integers in Pandas, you can use the `mode()`

function.

```
# Calculating the mode
mode_result = series.mode()
print("Calculating the mode(s):\n", mode_result)
```

In the above example, the mode of the series `[5, 10, 10, 15, 20, 20, 20, 25]`

is `20`

because it appears more frequently (three times) than any other value in the series, if there were multiple modes (i.e., more than one value with the same highest frequency), all modes would be returned. If all values appeared only once, an empty Series would be returned since there’s no mode.

Output.

## Finding the Mode of a Series of Strings

To find the mode of a Pandas Series containing strings, you can use the `mode()`

function just like you would with numeric values.

```
import pandas as pd
# Create a Pandas Series of strings
series_strings = pd.Series(['Pandas', 'Spark', 'Python', 'Spark', 'MongoDB', 'MongoDB', 'Spark'])
# Finding the mode of a series of strings
mode_result = series_strings.mode()
print("Mode(s) of the Series of strings:\n", mode_result)
# Output:
# Mode(s) of the Series of strings:
# 0 Spark
#dtype: object
```

In the above example, the mode of the Series of strings is `Spark`

because it appears more frequently (three times) than any other string in the Series, if there were multiple modes (i.e., more than one string with the same highest frequency), all modes would be returned.

## Finding the Mode of a Series with Missing Values

Let’s see how the mode of a series with missing values works in Pandas.

```
import pandas as pd
import numpy as np
# Creating a Pandas Series with missing values
series = pd.Series([10, 20, np.nan, 20, 30, 30, 40, np.nan])
# Calculating the mode of a series with missing values
mode_result = series.mode()
print("Calculating the mode(s):\n", mode_result)
# Output:
# Calculating the mode(s):
# 0 20.0
#1 30.0
#dtype: float64
```

In the above example, the mode of the series `[10, 20, np.nan, 20, 30, 30, 40, np.nan]`

is `20.0`

and `30.0`

. The `mode()`

function automatically handles missing values (`np.nan`

) and excludes them from the computation.

## Finding the Mode of a Series with Multiple Modes

Similarly, when you have multiple modes in a Series, it returns all values; if there are multiple values with the same highest frequency, all of them will be returned.

```
import pandas as pd
# Create a Pandas Series with multiple modes
series = pd.Series([10, 20, 20, 30, 30, 40, 40, 50, 50, 60, 60])
# Finding the mode of a series with multiple modes
mode_result = series.mode()
print("Mode(s) of the Series with multiple modes:\n", mode_result)
# Output:
# Mode(s) of the Series with multiple modes:
# 0 20
#1 30
#2 40
#3 50
#4 60
#dtype: int64
```

In the above example, the Series has multiple modes: 20, 30, 40, 50, and 60. Therefore, the `mode()`

function returns all of them as a pandas Series.

## Conclusion

In this article, I have explained the Pandas Series `mode()`

function and using its syntax, parameters, and usage how we can return the value(s) that appear most frequently in the Series. If there is a single mode, it returns that value. If there are multiple modes, it returns all of them.

Happy Learning!!

## Related Articles

- Pandas Series map() Function
- Pandas Series.clip() Function
- Pandas Series iloc[] Function
- Pandas series.str.get() Function
- Pandas Series round() Function
- Pandas Series.dtype() Function
- Pandas Series Drop duplicates() Function
- How to Make a Histogram in Pandas Series?