Maximum of DataFrame over an Axis
To find the maximum of the values over the requested axis in a DataFrame in Pandas, call max() method on this DataFrame. DataFrame.max() method returns a Series with values containing the maximum values over specified axis.
In this tutorial, we will learn the syntax of DataFrame.max() method, and how to use this method to find the maximum of values over a specified axis of DataFrame.
Syntax
The syntax of pandas DataFrame.max() method is
DataFrame.max(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
where
Parameter | Value | Description |
---|---|---|
axis | {index (0), columns (1)} | Axis for the function to be applied on. |
skipna | bool, default True | Exclude NA/null values when computing the result. |
level | int or level name, default None | If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series. |
numeric_only | bool, default None | Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series. |
**kwargs | Additional keyword arguments to be passed to the function. |
Return Value
- Series or
- DataFrame (if level specified)
Examples
Maximum in Columns of DataFrame
By default, maximum is calculated for columns in a DataFrame.
In the following program, we take a DataFrame two columns containing numerical data, and find the maximum in columns, or we can say over index axis of this DataFrame.
Example.py
import pandas as pd
df = pd.DataFrame({'a': [7, 4], 'b': [3, 2]})
result = df.max()
print(result)
Output
a 7
b 3
dtype: int64
Maximum in Rows of DataFrame
To compute the maximum values in rows of DataFrame, pass axis=1
in call to DataFrame.max() method.
Example.py
import pandas as pd
df = pd.DataFrame({'a': [7, 4], 'b': [3, 2]})
result = df.max(axis=1)
print(result)
Output
0 7
1 4
dtype: int64
Do not skip NA while finding Maximum
By default, NA values like None, np.nan, etc are ignored. But if we would like consider those values as well, pass skipna=False
to DataFrame.max() method.
Example.py
import pandas as pd
df = pd.DataFrame({'a': [7, None], 'b': [3, 2]})
result = df.max(skipna=False)
print(result)
Output
a NaN
b 3.0
dtype: float64
If any of the values is NA along the specified axis, then NaN
would be considered as maximum value.
Conclusion
In this Pandas Tutorial, we learned how to find the maximum of values over specified axis in a DataFrame using pandas DataFrame.max() method.