In this article, we are going to discuss how to find maximum value and its index position in columns and rows of a Dataframe.
DataFrame.max()
Pandas dataframe.max() method finds the maximum of the values in the object and returns it. If the input is a series, the method will return a scalar which will be the maximum of the values in the series. If the input is a dataframe, then the method will return a series with maximum of values over the specified axis in the dataframe. The index axis is the default axis taken by this method.
Syntax : DataFrame.max(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
Parameters :
axis : {index (0), columns (1)}
skipna : Exclude NA/null values when computing the result
level : If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series
numeric_only : Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.
Returns : max : Series or DataFrame (if level specified)
Let’s take an example of how to use this function. Let’s suppose we have a Dataframe.
How to Find Maximum Values of Every Column?
To find the maximum value of each column in a Pandas dataframe, we can use the max()
method. This method will return the maximum value of each column in the dataframe.
import pandas as pd df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) max_values = df.max() print(max_values)
Output:
A 3
B 6
C 9
dtype: int64
How to Find Maximum Values of Every Row?
To find the maximum value of each row in a Pandas dataframe, we can use the max()
method along the axis 1. This method will return the maximum value of each row in the dataframe.
import pandas as pd df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) max_values = df.max(axis=1) print(max_values)
Output:
0 7
1 8
2 9
dtype: int64
How to Find Maximum Values of Every Column Without Skipping NaN?
To find the maximum value of each column in a Pandas dataframe, including NaN values, we can use the max()
method with the skipna
parameter set to False
. This method will return the maximum value of each column in the dataframe, including NaN values.
import pandas as pd import numpy as np df = pd.DataFrame({'A': [1, 2, np.nan], 'B': [4, np.nan, 6], 'C': [7, 8, 9]}) max_values = df.max(skipna=False) print(max_values)
Output:
A NaN
B NaN
C 9.0
dtype: float64
Another useful method in pandas to find the maximum values of every row is the max()
method with the axis=1
parameter. This method will find the maximum value of each row in the DataFrame.
How to find maximum values of every row?
import pandas as pd df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) # find maximum values of every row max_values = df.max(axis=1) print(max_values)
Output:
0 7
1 8
2 9
dtype: int64
Now, what if there are NaN values in the DataFrame? By default, the max()
method skips NaN values. However, we can include them in the calculation by setting the skipna
parameter to False
. Here’s an example:
How to find maximum values of every column without skipping NaN?
import pandas as pd import numpy as np # create a sample DataFrame with NaN values df = pd.DataFrame({'A': [1, 2, np.nan], 'B': [4, np.nan, 6], 'C': [7, 8, 9]}) # find maximum values of every column without skipping NaN max_values = df.max(skipna=False) print(max_values)
Output:
A NaN
B NaN
C 9.0
dtype: float64
Now, what if we want to find the maximum values of a single column or selected columns only? We can pass the column name or a list of column names to the max()
method. Here’s an example:
How to find maximum values of a single column or selected columns?
import pandas as pd df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) # find maximum values of selected columns max_values = df[['A', 'C']].max() print(max_values)
Output:
A 3
C 9
dtype: int64
In addition to finding the maximum values, we can also get their positions in the DataFrame using the idxmax()
method. This method returns the index label of the first occurrence of the maximum value in each column or row. Here are some examples:
How to get position of maximum values of every column?
import pandas as pd df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) # get position of maximum values of every column max_positions = df.idxmax() print(max_positions)
Output:
A 2
B 2
C 2
dtype: int64
Last Updated on May 16, 2023 by admin