Pandas DataFrame - Maximum Value - max() - Exmaples
To find the maximum value of a Pandas DataFrame, you can use the pandas.DataFrame.max() method. Using max(), you can find the maximum value along an axis: row-wise or column-wise, or the maximum of the entire DataFrame.
In this tutorial, you'll learn how to find the maximum value in a DataFrame, along rows, columns, or the complete DataFrame using the DataFrame.max() method, with examples.
Examples
1. Find Maximum of DataFrame Along Columns
In this example, we will calculate the maximum values along the columns. We will find 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)
Explanation:
- The DataFrame contains marks for each subject.
- The
max()
method calculates the highest mark in each column, representing the maximum value for each subject. - The output displays the maximum value of each subject's marks.
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. This will show the highest marks obtained by each student across all subjects.
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 rows
mean = df_marks.max(axis=1)
print('\nMaximum Value\n------')
print(mean)
Explanation:
- The DataFrame contains marks for four students across three subjects.
- The
max(axis=1)
method calculates the highest mark for each student across the subjects (row-wise calculation). - The output shows the highest marks each student obtained across the subjects.
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.
To find the maximum value in the entire DataFrame, apply the max()
function to the result of the max()
function applied along columns or rows.
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)
Explanation:
- The DataFrame is created with marks of students across various subjects.
- The
max()
function first computes the maximum value along the columns (subject-wise). Then, applyingmax()
again on the result gives the maximum value across the entire DataFrame (overall maximum). - The result shows the highest value obtained in any of the cells of the DataFrame.
Output
DataFrame
----------
physics chemistry algebra
0 68 84 78
1 74 56 88
2 77 73 82
3 78 69 87
Maximum Value
------
88
4. Find Maximum Value with Specific Conditions (e.g., Ignoring NaN)
You can also calculate the maximum value of the DataFrame while ignoring NaN
values. This is useful when working with incomplete data.
Python Program
import pandas as pd
mydictionary = {'physics': [68, 74, 77, None],
'chemistry': [84, 56, None, 69],
'algebra': [78, 88, 82, 87]}
# Create DataFrame with NaN
df_marks = pd.DataFrame(mydictionary)
print('DataFrame\n----------')
print(df_marks)
# Calculate max while ignoring NaN
mean = df_marks.max(skipna=True)
print('\nMaximum Value Ignoring NaN\n------')
print(mean)
Explanation:
- This example uses the
skipna=True
argument to ignoreNaN
values when computing the maximum value in each column. - The result will show the highest value per subject, even if some data is missing.
- If
NaN
values were present, they would be excluded from the maximum calculation.
Output
DataFrame
----------
physics chemistry algebra
0 68.0 84.0 78
1 74.0 56.0 88
2 77.0 NaN 82
3 NaN 69.0 87
Maximum Value Ignoring NaN
------
physics 77.0
chemistry 84.0
algebra 88.0
dtype: float64
Summary
In this Pandas Tutorial, we have learned how to get the maximum value of a DataFrame, calculate the maximum value along columns, along rows, and the entire DataFrame. We also explored how to handle missing values (NaN) during the maximum value calculation.