The pandas.Series.min() function is used to find the minimum value in a Pandas Series. This function returns the smallest value in the Series, which can be useful for a variety of tasks, such as finding the lowest measurement or determining the smallest element in a data set.
In this article, I will explain the syntax of Series.min() function, its parameters, and how to find the minimum value of a given Series object.
Key Points –
- The
Series.min()function returns the minimum value in a Pandas Series. - By default,
min()excludesNaNvalues when calculating the minimum value, unlessskipna=Falseis specified. - It returns the minimum value found in the Series, which is of the same data type as the elements in the Series.
- The
skipnaparameter controls whetherNaNvalues are excluded (default isTrue). - When used with non-numerical data types, the minimum value is determined based on lexicographical (alphabetical) order.
- If
skipna=False, and the Series contains anyNaNvalues, the result will beNaN.
<a id="h-syntax-of-series-min-function"></a>
Syntax of Series.min() Function
Following is the syntax for creating Series.min() method.
# Syntax of Series.min() function
Series.min(axis=0, skipna=True, *args, **kwargs)
<a id="h-parameters-of-the-series-min-function"></a>
Parameters of the Series min() Function
Following are the parameters of the min() function.
axis- Default is0. This parameter is mainly used for compatibility with DataFrame, but for Series, it has no effect as Series is a one-dimensional structure.skipna- Boolean, default isTrue. IfTrue, it will excludeNaNvalues from the calculation of the minimum value. IfFalse, the result will beNaNif there are anyNaNvalues in the Series.args, kwargs- Additional arguments or keyword arguments that are passed to other functions (not commonly used for this specific function).
<a id="h-usage-of-series-min-function"></a>
Usage of Series.min() Function
The min() function returns the minimum value from a Pandas Series. It finds the smallest numerical or lexicographical value depending on the data type in the Series.
Now, let’s create a pandas series using a list of values and use the min() function.
import pandas as pd
import numpy as np
# Create a Series
series = pd.Series([25, 44, 66, 30, 12, 55, 20])
print("Original Series:\n",series)
Yields below output.
Here's an example demonstrating the basic use of the pandas.Series.min() function.
# Use Series.min() function
# To find the minimum value
min_value = series.min()
print("Minimum value in the Series:", min_value)
Here,
- The
pandas.Series.min()function is used to identify the smallest value in a Series. - The function scans through the Series and returns the minimum value it finds. In the example, the minimum value is
12.
<a id="h-series-with-negative-numbers"></a>
Series with Negative Numbers
The pandas.Series.min() function efficiently determines the smallest value in a Series, even when it includes negative numbers.
import pandas as pd
# Create a Pandas Series with negative and positive numbers
series = pd.Series([-7, 15, -3, 22, -12, 8, 0])
# Use Series.min() function
# To find the minimum value
result = series.min()
print("Minimum value in the Series with Negative Numbers:", result)
# Output:
# Minimum value in the Series with Negative Numbers: -12
Here,
- A Pandas Series is created with a mix of negative and positive numbers:
[-7, 15, -3, 22, -12, 8, 0]. - The
series.min()function is used to find the minimum value in the Series. The smallest number in the Series is-12, so the result is-12.
<a id="h-series-with-nan-values-default-skipna-true"></a>
Series With NaN Values (Default skipna=True)
By default, the min() function in Pandas Series uses skipna=True, which automatically excludes NaN (Not a Number) values when calculating the minimum. This ensures that if a Series contains NaN values, they are ignored, and the minimum value is determined based on the non-NaN elements.
import pandas as pd
import numpy as np
# Create a Pandas Series with NaN values
series_with_nan = pd.Series([7, 15, np.nan, 3, 12, np.nan, 9])
# Use Series.min() function
# To find the minimum value (skipping NaN values by default)
result = series_with_nan.min()
print("Minimum value in the Series with NaN values (skipna=True):", result)
# Output:
# Minimum value in the Series with NaN values (skipna=True): 3.0
Here,
- A Pandas Series is created with some
NaNvalues:[10, 25, NaN, 6, 18, NaN, 45]. - The
series_with_nan.min()function is called to find the minimum value. - By default,
skipna=True, soNaNvalues are excluded from the calculation, and the function returns3.0as the smallest value in the Series, ignoring theNaNvalues.
<a id="h-series-with-skipna-false-to-include-nan"></a>
Series With skipna=False to Include NaN
When using the min() function in Pandas Series, setting skipna=False ensures that NaN values are included in the calculation. If the Series contains NaN values, the result will be NaN as well. It's important to remember that NaN represents a missing value in Python. For more information on handling NaN values, refer to the handling missing values in Pandas.
# Use Series.min() function
# To find the minimum value (including NaN values)
result = series_with_nan.min(skipna=False)
print("Minimum value in the Series with NaN values (skipna=False):", result)
# Output:
# Minimum value in the Series with NaN values (skipna=False): nan
Here,
- A Pandas Series is created with values that include
NaN:[7, 15, NaN, 3, 12, NaN, 9]. - The
series_with_nan.min(skipna=False)function is used to find the minimum value, specifyingskipna=False. - Since
skipna=Falseis set, theNaNvalues are included in the calculation. As a result, if anyNaNvalues are present in the Series, the function will returnNaN. - The output is
nan, indicating that the calculation includesNaNand doesn't skip them.
<a id="h-faq-on-pandas-series-min-function"></a>
FAQ on Pandas Series.min() Function
The pandas Series.min() function returns the minimum value in a Series. It scans through the elements of the Series and identifies the smallest value based on the data type
By default, NaNs are ignored when finding the minimum. If all values are NaN, the result will be NaN.
You cannot directly include NaNs in the computation of min() because NaN is treated as missing data. However, you can replace NaNs with a specific value before applying min().
You can find the minimum value for a subset of rows in a pandas Series by filtering the Series before applying the min() function.
It works with strings or dates in a Series. The minimum is determined lexicographically for strings and chronologically for datetime objects.
<a id="h-conclusion"></a>
Conclusion
In conclusion, the pandas.Series.min() function is a powerful and flexible tool for finding the smallest value in a Series. By default, it excludes NaN values using the skipna=True parameter, ensuring that the calculation focuses only on valid (non-NaN) data. However, by setting skipna=False, you can include NaN values in the calculation, resulting in NaN if any are present.
Happy Learning !!
Related Articles
- Pandas Series count() Function
- Pandas Series.rolling() Function
- Pandas Series rank() Function
- Pandas Series mode() Function
- Pandas Series sample() Function
- Pandas Series reset_index() Function
- Pandas Series reset_index() Function
- Pandas Series.str.contains() With Examples
- Pandas Series Drop duplicates() Function
- How to Make a Histogram in Pandas Series?
- Pandas Series.value_counts() With Examples
<a id="h-references"></a>
