Signup/Sign In

Pandas DataFrame max() Method

In this tutorial, we will discuss and learn the Python pandas DataFrame.max() method. This method can be used to get the maximum of the values over the requested axis. It returns Series and if the level is specified, it returns the DataFrame.

The below shows the syntax of the DataFrame.max() method.

Syntax

DataFrame.max(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)

Parameters

axis: It represents index or column axis, '0' for index and '1' for the column. When the axis=0, function applied over the index axis and when the axis=1 function applied over the column axis.

skipna: It represents the bool(True or False). The default value is None. If this parameter is True, it excludes all NA/null values when computing the result.

level: It represents the int or level name, the default value is None. It counts along with a particular level, if the DataFrame is a Multi index, collapsing into a Series.

numeric_only: bool(True or False), the default is None. If this parameter is True, it includes only float, int, boolean columns.

**kwargs: Additional keyword arguments to be passed to the function.

Example: Find maximum values of the DataFrame

Let's create a DataFrame and get the maximum value over the index axis by assigning parameter axis=0 in the DataFrame.max() method. See the below example.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A":[0,52,78],"B":[77,45,96],"C":[16,23,135],"D":[17, 22, 56]}) 
print("------The DataFrame is------")
print(df)
print("---------------------------")
print(df.max(axis=0))


------The DataFrame is------
A B C D
0 0 77 16 17
1 52 45 23 22
2 78 96 135 56
---------------------------
A 78
B 96
C 135
D 56
dtype: int64

Example: Find maximum values of the DataFrame using the DataFrame.max() Method over the column axis

Let's create a DataFrame and get the maximum value over the column axis by assigning parameter axis=1 in the DataFrame.max() method. See the below example.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A":[0,52,78],"B":[77,45,96],"C":[16,23,135],"D":[17, 22, 56]}) 
print("------The DataFrame is------")
print(df)
print("---------------------------")
print(df.max(axis=1))


------The DataFrame is------
A B C D
0 0 77 16 17
1 52 45 23 22
2 78 96 135 56
---------------------------
0 77
1 52
2 135
dtype: int64

Example: Find maximum values of the DataFrame including null values

Let's create a DataFrame with null values and get the maximum value over the index axis excluding null values by passing parameter skipna=False in the DataFrame.max() method. It includes all NA/null values when computing the results. See the below example.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A":[0,None,78],"B":[77,45,None],"C":[16,23,None],"D":[17, 22, 56]}) 
print("------The DataFrame is------")
print(df)
print("---------------------------")
print(df.max(axis=0,skipna=False))


------The DataFrame is------
A B C D
0 0.0 77.0 16.0 17
1 NaN 45.0 23.0 22
2 78.0 NaN NaN 56
---------------------------
A NaN
B NaN
C NaN
D 56.0
dtype: float64

Example: Find maximum values excluding null values

Let's create a DataFrame with null values and get the maximum value over the index axis excluding null values by passing parameter skipna=True in the DataFrame.max() method. It excludes all NA/null values when computing the results. See the below example.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A":[0,None,78],"B":[77,45,None],"C":[16,23,None],"D":[17, 22, 56]}) 
print("------The DataFrame is------")
print(df)
print("---------------------------")
print(df.max(axis=0,skipna=True))


------The DataFrame is------
A B C D
0 0.0 77.0 16.0 17
1 NaN 45.0 23.0 22
2 78.0 NaN NaN 56
---------------------------
A 78.0
B 77.0
C 23.0
D 56.0
dtype: float64

Conclusion

In this tutorial, we learned the Python pandas DataFrame.max() method. We learned the syntax, parameters and applying this method on the DataFrame to understand the DataFrame.max() method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.