Select rows with max value in Pandas DataFrame

In this post, we will learn how to select rows with max value in Pandas dataframe by using the built dataframe class member function max().To use the Pandas library first we have to install it on the local system by using the pip command “pip install pandas” and import it into our code by using “import pandas as pd” to use its functions.

1. Select all rows with max value Pandas Dataframe


The Pandas dataframe max() function is used to find the maximum value in a given object. The given object can be a series or a Dataframe. If the given object is a dataframe that it will return the maximum value along with the axis. The default axis is the index axis.

In this python example we will understand how to select rows with max value Pandas DataFrame.We are finding the max value in each row of the dataframe by using the max() function by simply calling the max() function with the DataFrame object.

import pandas as pd

   
Student_dict = {
    'Amount': [600.0,np.nan,700, np.nan],
    'Marks':[70,100, 180,200],
    'fee': [200, 500, 100, 350]
}
   
df = pd.DataFrame(Student_dict)
 
print(df)
 
maxval = df.max()

print('\n Max value in Dataframe:\n ',maxval)


Output

   Amount  Marks  fee
0   600.0     70  200
1     NaN    100  500
2   700.0    180  100
3     NaN    200  350

 Max value in Dataframe:
  Amount    700.0
Marks     200.0
fee       500.0
dtype: float64

2. Select rows with max value in Pandas dataframe based on a column


In this example, we are finding the max() value of ”Amount’ column of Pandas DataFrame.The maximum amount and based on that other columns is selected values as well.

import pandas as pd

   
Student_dict = {
    'Amount': [600.0,np.nan,700, np.nan],
    'Marks':[70,100, 180,200],
    'fee': [200, 500, 100, 350]
}
   
df = pd.DataFrame(Student_dict)
 
print(df)
 
maxval = df[df.Amount == df.Amount.max()]



print('\n Max value in Dataframe:\n ',maxval)


Output

 Amount  Marks  fee
0   600.0     70  200
1     NaN    100  500
2   700.0    180  100
3     NaN    200  350

 Max value in Dataframe:
     Amount  Marks  fee
2   700.0    180  100

3. Select rows with max values in Pandas dataframe


Instead of selecting all columns values based on single column maximum value.In this example we are selecting the maximum amount value in ‘Amount’ column by using code “df.Amount.max()”

import pandas as pd

   
Student_dict = {
    'Amount': [600.0,np.nan,700, np.nan],
    'Marks':[70,100, 180,200],
    'fee': [200, 500, 100, 350]
}
   
df = pd.DataFrame(Student_dict)
 
print(df)
 
maxval = df.Amount.max()



print('\n Max amount in Dataframe:\n ',maxval)


Output

 Amount  Marks  fee
0   600.0     70  200
1     NaN    100  500
2   700.0    180  100
3     NaN    200  350

 Max amount in Dataframe:
  700.0

4. Select rows with max values Pandas dataframe


In this example we are selecting the rows from datafame based on max values of column ‘Marks’.The rows will be select where values of Marks is equal to maximum value in Marks columns.Let us understand with below example.

#python 3  select rows with max values in Pandas datafram

import pandas as pd

Student_dict = {
    'Amount': [600.0,np.nan,700, np.nan],
    'Marks':[70,100, 180,200],
    'fee': [200, 500, 100, 350]
}
 
   
df = pd.DataFrame(Student_dict)
 
print(df)
 
maxval = df[df.Marks == df.Marks.max()]



print('\n Max amount in Dataframe:\n ',maxval)



Output

   Amount  Marks  fee
0   600.0     70  200
1     NaN    100  500
2   700.0    180  100
3     NaN    200  350

 Max amount in Dataframe:
     Amount  Marks  fee
3     NaN    200  350

Summary

In this post we have learned how to select rows with max value in Pandas dataframe with examples by using built in function max()