Built with Tutify.io

    AIStudyLabs

    Pandas Series.min() Function

    Pandas Series.min() Function

    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 –

    <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.

    <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,

    <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 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 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 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

    <a id="h-references"></a>

    References