Built with Tutify.io

    AIStudyLabs

    Pandas Series.max() Function

    Pandas Series.max() Function

    The max() function in Pandas Series is used to find the maximum value within a Series. It returns the highest value present in the Series. It returns a float value representing the maximum value within the series. This function always returns a Series even if only one value is present.

    In this article, I will explain the syntax of Series.max() function, its parameters, and how to find the maximum value of a given Series object.

    Key Points –

    <a id="h-syntax-of-series-max-method"></a>

    Syntax of Series.max() Method

    Following is the syntax for creating Series.max() method.

    
    # Syntax of Series.max() function
    Series.max(axis=_NoDefault.no_default, skipna=True, level=None, numeric_only=None, **kwargs)
    

    Following are the parameters of the max().

    Use Series.max() Function

    Series.max() function is used to get the max of the values in pandas. This function gets the maximum value of the given object elements in Pandas.

    A Pandas Series is a one-dimensional, index-labeled data structure provided by the Pandas library. It's capable of storing various data types including strings, integers, floats, and other Python objects. Each element in the Series can be accessed using its corresponding default indices.

    Now, let’s create pandas series using a list of values and use the max() function.

    
    import pandas as pd
    import numpy as np
    
    # Create a Series
    ser = pd.Series([25, 44, 66, 30, 12, 55, 20])
    
    # Use Series.max() function
    ser2 = ser.max()
    print(ser2)
    
    # Output:
    # 66
    

    <a id="h-pandas-series-max-ignore-nan"></a>

    Pandas Series Max Ignore NaN

    By default, the max() function in Pandas Series uses skipna=True, meaning it automatically excludes NaN (Not a Number) values when calculating the maximum value. This ensures that if a series contains NaN values, they are automatically ignored, and the maximum value is computed based on the non-NaN elements of the series.

    
    # Pandas series max ignore nan 
    ser = pd.Series([15.4, 28.2, None, 50.3, 94.6, None, 66.9, 81.7, np.nan])
    ser2 = ser.max(skipna = True)
    print(ser2)
    
    # Output:
    # 94.6
    

    In the above example, the max() function automatically ignores NaN values and computes the maximum among the non-NaN values in the series, resulting in the output 94.6.

    Similarly, when using the max() function in Pandas Series, setting skipna=False ensures that NaN values are not ignored. In this case, if the series contains NaN values, the result will be NaN as well. It's important to note that NaN represents a missing value in Python, and for more examples on NaN refer to handling missing values in Pandas.

    
    # Pandas series max ignore nan use skipna = False
    ser = pd.Series([15.4, 28.2, None, 50.3, 94.6, None, 66.9, 81.7, np.nan])
    ser2 = ser.max(skipna = False)
    print(ser2)
    
    # Output:
    # nan
    

    You can also follow the below program to get the maximum value in the pandas Series object. Here, I am using the multiindex.

    
    import pandas as pd
    import numpy as np
    
    inx = pd.MultiIndex.from_arrays([
        ['Bigdata','Bigdata','ProgramLanguage','ProgramLanguage'],
        ['Spark','PySpark','Java','JavaScript']],
        names=['Technology', 'Course'])
    
    ser = pd.Series([2000, 3500, 2500, 1000], name='Courses', index=inx)
    print(ser)
    
    # Output:
    # Technology       Course    
    # Bigdata          Spark         2000
    #                  PySpark       3500
    # ProgramLanguage  Java          2500
    #                  JavaScript    1000
    # Name: Courses, dtype: int64
    
    # Use series.max() function
    ser2 = ser.max()
    print(ser2)
    
    # Output:
    # 3500
    

    <a id="h-complete-example-of-series-max-method"></a>

    Complete Example of Series.max() Method

    
    import pandas as pd
    import numpy as np
    
    # Create a Series
    ser = pd.Series([25, 44, 66, 30, 12, 55, 20])
    print(ser)
    
    # Use Series.max() function
    ser2 = ser.max()
    print(ser2)
    
    # Pandas series max ignore nan 
    ser = pd.Series([15.4, 28.2, None, 50.3, 94.6, None, 66.9, 81.7, np.nan])
    ser2 = ser.max(skipna = True)
    print(ser2)
    
    # Pandas series max ignore nan use skipna = False
    ser = pd.Series([15.4, 28.2, None, 50.3, 94.6, None, 66.9, 81.7, np.nan])
    ser2 = ser.max(skipna = False)
    print(ser2)
    

    <a id="h-faq-on-series-max-method"></a>

    FAQ on Series.max() Method

    The Pandas Series.max() function is used to find the maximum value within a Pandas Series. It returns the highest value present in the Series. By default, it ignores any NaN (missing) values in the Series while computing the maximum value.

    Pandas handles NaN (Not a Number) values with the Series.max() function by defaulting to skipping them during the computation of the maximum value. This means that NaN values are ignored, and only the non-NaN values in the Series are considered when determining the maximum value. If all values in the Series are NaN or if NaN values are the maximum, the result of Series.max() will be NaN. However, you can override this behavior by setting the skipna parameter to False, in which case NaN values will be considered in the computation, possibly resulting in a NaN maximum if any NaN values are present.

    If all values in the Series are NaN (Not a Number), the result of the Series.max() function will be NaN. This is because the function defaults to skipping NaN values during the computation of the maximum value, and if all values are NaN, there are no valid values to compute the maximum from, resulting in NaN as the output.

    You can also use the numpy.max() function directly on the Series or the .max() method of NumPy arrays if the Series is converted.

    Conclusion

    In conclusion, the max() function in Pandas Series is a valuable tool for obtaining the maximum value within a Series object. By default, it ignores NaN values, ensuring accurate calculations. However, by setting skipna=False, NaN values can be included in the computation.

    Happy Learning !!

    Related Articles

    References