In this article, we will discuss how to find maximum value & position in rows or columns of a Dataframe and its index position.
- DataFrame.max()
- Syntax
- Get maximum values in every row & column of the Dataframe
- Get maximum values of every column
- Get maximum values of every row
- Get maximum values of every column without skipping NaN
- Get maximum values of a single column or selected columns
- Get row index label or position of maximum values of every column
- Get row index label of Maximum value in every column
- Get Column names of Maximum value in every row
DataFrame.max()
Python pandas provide a member function in the dataframe to find the maximum value.
Syntax:
DataFrame.max(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
Dataframe.max() accepts these arguments:
axis: Where max element will be searched
skipna: Default is True means if not provided it will be skipped.
Let’s create a dataframe,
import pandas as pd import numpy as np # List of Tuples matrix = [(17, 15, 12), (53, np.NaN, 10), (46, 34, 11), (35, 45, np.NaN), (76, 26, 13) ] # Create a DataFrame object dfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz')) print(dfObj)
Output:
x     y   z a 17   15.0  12.0 b 53   NaN  10.0 c 46   34.0  11.0 d 35   45.0  NaN e 76   26.0  13.0
Get maximum values in every row & column of the Dataframe
Here, you will find two ways to get the maximum values in dataframe
Also Check:Â
- Python: How to get the current date and time or timestamp?
- Python: Find indexes of an element in pandas dataframe
Get maximum values of every column
In this, we will call the max() function to find the maximum value of every column in DataFrame.
import pandas as pd import numpy as np # List of Tuples matrix = [(17, 15, 12), (53, np.NaN, 10), (46, 34, 11), (35, 45, np.NaN), (76, 26, 13) ] # Create a DataFrame object dfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz')) # Get a series containing maximum value of each column maxValuesObj = dfObj.max() print('Maximum value in each column : ') print(maxValuesObj)
Output:
Maximum value in each column : x 76.0 y 45.0 z 13.0
Get maximum values of every row
In this also we will call the max() function to find the maximum value of every row in DataFrame.
import pandas as pd import numpy as np # List of Tuples matrix = [(17, 15, 12), (53, np.NaN, 10), (46, 34, 11), (35, 45, np.NaN), (76, 26, 13) ] # Create a DataFrame object dfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz')) # Get a series containing maximum value of each row maxValuesObj = dfObj.max(axis=1) print('Maximum value in each row : ') print(maxValuesObj)
Output:
Maximum value in each row : a  17.0 b  53.0 c  46.0 d  45.0 e  76.0
So in the above example, you can see that it returned a series with a row index label and maximum value of each row.
Get maximum values of every column without skipping NaN
import pandas as pd import numpy as np # List of Tuples matrix = [(17, 15, 12), (53, np.NaN, 10), (46, 34, 11), (35, 45, np.NaN), (76, 26, 13) ] # Create a DataFrame object dfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz')) # Get a series containing maximum value of each column without skipping NaN maxValuesObj = dfObj.max(skipna=False) print('Maximum value in each column including NaN: ') print(maxValuesObj)
Output:
Maximum value in each column including NaN: x 76.0 y NaN z NaN
So in the above example, you can see that we have passed the ‘skipna=False’ in the max() function, So it included the NaN while searching for NaN.
If there is any NaN in the column then it will be considered as the maximum value of that column.
Get maximum values of a single column or selected columns
So for getting a single column maximum value we have to select that column and apply the max() function in it,
import pandas as pd import numpy as np # List of Tuples matrix = [(17, 15, 12), (53, np.NaN, 10), (46, 34, 11), (35, 45, np.NaN), (76, 26, 13) ] # Create a DataFrame object dfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz')) # Get maximum value of a single column 'y' maxValue = dfObj['y'].max() print("Maximum value in column 'y': " , maxValue)
Here you can see that we have passed y maxValue = dfObj['y'].max()
for getting max value in that column.
Output:
Maximum value in column 'y': 45.0
We can also pass the list of column names instead of passing single column like.,
import pandas as pd import numpy as np # List of Tuples matrix = [(17, 15, 12), (53, np.NaN, 10), (46, 34, 11), (35, 45, np.NaN), (76, 26, 13) ] # Create a DataFrame object dfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz')) # Get maximum value of a single column 'y' maxValue = dfObj[['y', 'z']].max() print("Maximum value in column 'y' & 'z': ") print(maxValue)
Output:
Maximum value in column 'y' & 'z': y 45.0 z 13.0
Get row index label or position of maximum values of every column
DataFrame.idxmax()
So in the above examples, you have seen how to get the max value of rows and columns but what if we want to know the index position of that row and column whereas the value is maximum, by using dataframe.idxmax() we get the index position.
Syntax-
DataFrame.idxmax(axis=0, skipna=True)
Get row index label of Maximum value in every column
import pandas as pd import numpy as np # List of Tuples matrix = [(17, 15, 12), (53, np.NaN, 10), (46, 34, 11), (35, 45, np.NaN), (76, 26, 13) ] # Create a DataFrame object dfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz')) # get the index position of max values in every column maxValueIndexObj = dfObj.idxmax() print("Max values of columns are at row index position :") print(maxValueIndexObj)
Output:
Max values of columns are at row index position : x e y d z e dtype: object
So here you have seen it showed the index position of the column where max value exists.
Get Column names of Maximum value in every row
import pandas as pd import numpy as np # List of Tuples matrix = [(17, 15, 12), (53, np.NaN, 10), (46, 34, 11), (35, 45, np.NaN), (76, 26, 13) ] # Create a DataFrame object dfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz')) # get the column name of max values in every row maxValueIndexObj = dfObj.idxmax(axis=1) print("Max values of row are at following columns :") print(maxValueIndexObj)
Output:
Max values of row are at following columns : a x b x c x d y e x dtype: object
So here you have seen it showed the index position of a row where max value exists.
Conclusion:
So in this article, we have seen how to find maximum value & position in rows or columns of a Dataframe and its index position. Thank you!
Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.
Read more Articles on Python Data Analysis Using Padas – Find Elements in a Dataframe
- Check if a value exists in a DataFrame using in & not in operator | isin()
- Find & Drop duplicate columns in a DataFrame
- Check if a DataFrame is empty in Python
- Find duplicate rows in a Dataframe based on all or selected columns using DataFrame.duplicated() in Python
- Find indexes of an element in pandas dataframe