Pandas DataFrame - Maximum Value - max() - Exmaples


To find the maximum value of a Pandas DataFrame, you can use pandas.DataFrame.max() method. Using max(), you can find the maximum value along an axis: row wise or column wise, or maximum of the entire DataFrame.

In this tutorial, you'll learn how to find the maximum value in a DataFrame, along rows, columns, or complete DataFrame using DataFrame.max() method, with examples.

Examples

1. Find maximum of DataFrame along columns

In this example, we will calculate the maximum along the columns. We will come to know the highest marks obtained by students, subject wise.

Python Program

import pandas as pd

mydictionary = {'physics': [68, 74, 77, 78],
     'chemistry': [84, 56, 73, 69],
     'algebra': [78, 88, 82, 87]}

# Create DataFrame 
df_marks = pd.DataFrame(mydictionary)
print('DataFrame\n----------')
print(df_marks)

# Calculate max along columns
mean = df_marks.max()
print('\nMaximum Value\n------')
print(mean)

Output

DataFrame
----------
   physics  chemistry  algebra
0       68         84       78
1       74         56       88
2       77         73       82
3       78         69       87

Maximum Value
------
physics      78
chemistry    84
algebra      88
dtype: int64

2. Find maximum of DataFrame along Rows

In this example, we will find the maximum along rows of the DataFrame. It results in finding the maximum marks obtained by the student for any subject.

Python Program

import pandas as pd

mydictionary = {'physics': [68, 74, 77, 78],
     'chemistry': [84, 56, 73, 69],
     'algebra': [78, 88, 82, 87]}

# Create DataFrame 
df_marks = pd.DataFrame(mydictionary)
print('DataFrame\n----------')
print(df_marks)

# Calculate max along columns
mean = df_marks.max(axis=1)
print('\nMaximum Value\n------')
print(mean)

Output

DataFrame
----------
   physics  chemistry  algebra
0       68         84       78
1       74         56       88
2       77         73       82
3       78         69       87

Maximum Value
------
0    84
1    88
2    82
3    87
dtype: int64

3. Maximum value of complete DataFrame

In this example, we will find out the maximum value in a DataFrame irrespective of rows or columns.

In the previous examples, we have found maximum value along columns and rows respectively. Apply max() function to the result of the max() function in those cases. You will get the maximum of complete DataFrame.

Python Program

import pandas as pd

mydictionary = {'physics': [68, 74, 77, 78],
     'chemistry': [84, 56, 73, 69],
     'algebra': [78, 88, 82, 87]}

# Create dataframe
df_marks = pd.DataFrame(mydictionary)
print('DataFrame\n----------')
print(df_marks)

# Calculate max of whole DataFrame
mean = df_marks.max().max()
print('\nMaximum Value\n------')
print(mean)

Output

DataFrame
----------
   physics  chemistry  algebra
0       68         84       78
1       74         56       88
2       77         73       82
3       78         69       87

Maximum Value
------
88

Summary

In this Pandas Tutorial, we have learned how to get maximum value of whole DataFrame, get maximum value of DataFrame along column(s) and obtain maximum value of DataFrame along rows.