The Pandas Series.max()
function is used to get the maximum of the values. It returns a float value representing the max of 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 get the max values of a given Series object with examples.
1. Syntax of Series.max() Function
Following is the syntax for creating Series.max()function.
# 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().
axis
– {index (0)}: Specify the axis for the function to be applied on. For Series, this parameter is unused and defaults to 0.skipna
– bool, default True: Excludes all None/NaN from the mean/computing the result. Default set to Truelevel
– Use with multiindex. Takes int or level name, default Nonenumeric_only
– Excludes all non-numeric values. Considers only int, float & boolean. Default None**kwargs
– Additional keyword arguments to be passed to the function.
2. 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.
Pandas Series is a one-dimensional, Index-labeled data structure available in the Pandas library. It can store all the datatypes such as strings, integers, float, and other python objects. We can access each element in the Series with the help of 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
3. Pandas Series Max Ignore NaN
By default, the max() function uses skipna=True
meaning it ignores the NaN (Not a Number) values when finding the maximum value. If a series contains NaN values, they are automatically ignored and get the maximum value of the given series object. For examples,
# 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
You can also use the skipna=False
to not ignore NaN values, and if you have Nan values in the series it returns nan values. NaN is considered 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
4. Complete Example of Series.max() Function
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)
5. Conclusion
In this article, I have explained the pandas series max()
function that returns the maximum value of a given series object with examples.
Happy Learning !!
Related Articles
- Pandas Series.mean() Function
- Pandas Series.fillna() Function
- How to Reshape Pandas Series
- How to Rename a Pandas Series
- Pandas Get Floor or Ceil of Series
- How to Convert List to Pandas Series
- Pandas Remove Elements From Series
- Create a Set From a Series in Pandas
- Check Values of Pandas Series is Unique
- Pretty Print Pandas DataFrame or Series
- Add Column Name to Pandas Series
- How to Convert Pandas to PySpark DataFrame
- Pandas Series.isin() Function
- Pandas.Series.combine()
- Pandas Rolling Sum