Select row with maximum and minimum value in Pandas dataframe

Let’s see how can we select row with maximum and minimum value in Pandas dataframe with help of different examples.

Consider this dataset.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas and numpy
import pandas as pd
import numpy as np
  
# data of 2018 drivers world championship
dict1 ={'Driver':['Hamilton', 'Vettel', 'Raikkonen',
                  'Verstappen', 'Bottas', 'Ricciardo',
                  'Hulkenberg', 'Perez', 'Magnussen'
                  'Sainz', 'Alonso', 'Ocon', 'Leclerc',
                  'Grosjean', 'Gasly', 'Vandoorne',
                  'Ericsson', 'Stroll', 'Hartley', 'Sirotkin'],
                    
        'Points':[408, 320, 251, 249, 247, 170, 69, 62, 56,
                   53, 50, 49, 39, 37, 29, 12, 9, 6, 4, 1],
                     
        'Age':[33, 31, 39, 21, 29, 29, 31, 28, 26, 24, 37,
                      22, 21, 32, 22, 26, 28, 20, 29, 23]}
                        
# creating dataframe using DataFrame constructor
df = pd.DataFrame(dict1)
print(df.head(10))

chevron_right


Output:

Using max on Dataframe –

Code #1: Shows max on Driver, Points, Age columns.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas and numpy
import pandas as pd
import numpy as np
  
# data of 2018 drivers world championship
dict1 ={'Driver':['Hamilton', 'Vettel', 'Raikkonen',
                  'Verstappen', 'Bottas', 'Ricciardo',
                  'Hulkenberg', 'Perez', 'Magnussen'
                  'Sainz', 'Alonso', 'Ocon', 'Leclerc',
                  'Grosjean', 'Gasly', 'Vandoorne',
                  'Ericsson', 'Stroll', 'Hartley', 'Sirotkin'],
                    
        'Points':[408, 320, 251, 249, 247, 170, 69, 62, 56,
                   53, 50, 49, 39, 37, 29, 12, 9, 6, 4, 1],
                     
        'Age':[33, 31, 39, 21, 29, 29, 31, 28, 26, 24, 37,
                      22, 21, 32, 22, 26, 28, 20, 29, 23]}
                        
# creating dataframe using DataFrame constructor
df = pd.DataFrame(dict1)
  
# the result shows max on
# Driver, Points, Age columns.
print(df.max())

chevron_right


Output:

 
Code #2: Who scored max points



filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas and numpy
import pandas as pd
import numpy as np
  
# data of 2018 drivers world championship
dict1 ={'Driver':['Hamilton', 'Vettel', 'Raikkonen',
                  'Verstappen', 'Bottas', 'Ricciardo',
                  'Hulkenberg', 'Perez', 'Magnussen'
                  'Sainz', 'Alonso', 'Ocon', 'Leclerc',
                  'Grosjean', 'Gasly', 'Vandoorne',
                  'Ericsson', 'Stroll', 'Hartley', 'Sirotkin'],
                    
        'Points':[408, 320, 251, 249, 247, 170, 69, 62, 56,
                   53, 50, 49, 39, 37, 29, 12, 9, 6, 4, 1],
                     
        'Age':[33, 31, 39, 21, 29, 29, 31, 28, 26, 24, 37,
                      22, 21, 32, 22, 26, 28, 20, 29, 23]}
                        
# creating dataframe using DataFrame constructor
df = pd.DataFrame(dict1)
  
# Who scored more points ?
print(df[df.Points == df.Points.max()])

chevron_right


Output:

 

Code #3: What is the maximum age

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas and numpy
import pandas as pd
import numpy as np
  
# data of 2018 drivers world championship
dict1 ={'Driver':['Hamilton', 'Vettel', 'Raikkonen',
                  'Verstappen', 'Bottas', 'Ricciardo',
                  'Hulkenberg', 'Perez', 'Magnussen'
                  'Sainz', 'Alonso', 'Ocon', 'Leclerc',
                  'Grosjean', 'Gasly', 'Vandoorne',
                  'Ericsson', 'Stroll', 'Hartley', 'Sirotkin'],
                    
        'Points':[408, 320, 251, 249, 247, 170, 69, 62, 56,
                   53, 50, 49, 39, 37, 29, 12, 9, 6, 4, 1],
                     
        'Age':[33, 31, 39, 21, 29, 29, 31, 28, 26, 24, 37,
                      22, 21, 32, 22, 26, 28, 20, 29, 23]}
                        
# creating dataframe using DataFrame constructor
df = pd.DataFrame(dict1)
  
# what is the maximum age ?
print(df.Age.max())

chevron_right


Output:

Code #4: Which row has maximum age in the dataframe | who is the oldest driver ?

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas and numpy
import pandas as pd
import numpy as np
  
# data of 2018 drivers world championship
dict1 ={'Driver':['Hamilton', 'Vettel', 'Raikkonen',
                  'Verstappen', 'Bottas', 'Ricciardo',
                  'Hulkenberg', 'Perez', 'Magnussen'
                  'Sainz', 'Alonso', 'Ocon', 'Leclerc',
                  'Grosjean', 'Gasly', 'Vandoorne',
                  'Ericsson', 'Stroll', 'Hartley', 'Sirotkin'],
                    
        'Points':[408, 320, 251, 249, 247, 170, 69, 62, 56,
                   53, 50, 49, 39, 37, 29, 12, 9, 6, 4, 1],
                     
        'Age':[33, 31, 39, 21, 29, 29, 31, 28, 26, 24, 37,
                      22, 21, 32, 22, 26, 28, 20, 29, 23]}
                        
# creating dataframe using DataFrame constructor
df = pd.DataFrame(dict1)
  
# Which row has maximum age |
# who is the oldest driver ?
print(df[df.Age == df.Age.max()])

chevron_right


Output:

Using min on Dataframe –

Code #1: Shows min on Driver, Points, Age columns.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas and numpy
import pandas as pd
import numpy as np
  
# data of 2018 drivers world championship
dict1 ={'Driver':['Hamilton', 'Vettel', 'Raikkonen',
                  'Verstappen', 'Bottas', 'Ricciardo',
                  'Hulkenberg', 'Perez', 'Magnussen'
                  'Sainz', 'Alonso', 'Ocon', 'Leclerc',
                  'Grosjean', 'Gasly', 'Vandoorne',
                  'Ericsson', 'Stroll', 'Hartley', 'Sirotkin'],
                    
        'Points':[408, 320, 251, 249, 247, 170, 69, 62, 56,
                   53, 50, 49, 39, 37, 29, 12, 9, 6, 4, 1],
                     
        'Age':[33, 31, 39, 21, 29, 29, 31, 28, 26, 24, 37,
                      22, 21, 32, 22, 26, 28, 20, 29, 23]}
                        
# creating dataframe using DataFrame constructor
df = pd.DataFrame(dict1)
  
# the result shows min on 
# Driver, Points, Age columns.
print(df.min())

chevron_right


Output:

 

Code #2: Who scored less points

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas and numpy
import pandas as pd
import numpy as np
  
# data of 2018 drivers world championship
dict1 ={'Driver':['Hamilton', 'Vettel', 'Raikkonen',
                  'Verstappen', 'Bottas', 'Ricciardo',
                  'Hulkenberg', 'Perez', 'Magnussen'
                  'Sainz', 'Alonso', 'Ocon', 'Leclerc',
                  'Grosjean', 'Gasly', 'Vandoorne',
                  'Ericsson', 'Stroll', 'Hartley', 'Sirotkin'],
                    
        'Points':[408, 320, 251, 249, 247, 170, 69, 62, 56,
                   53, 50, 49, 39, 37, 29, 12, 9, 6, 4, 1],
                     
        'Age':[33, 31, 39, 21, 29, 29, 31, 28, 26, 24, 37,
                      22, 21, 32, 22, 26, 28, 20, 29, 23]}
                        
# creating dataframe using DataFrame constructor
df = pd.DataFrame(dict1)
  
# Who scored less points ?
print(df[df.Points == df.Points.min()])

chevron_right


Output:

 
Code #3: Which row has minimum age in the dataframe | who is the youngest driver

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas and numpy
import pandas as pd
import numpy as np
  
# data of 2018 drivers world championship
dict1 ={'Driver':['Hamilton', 'Vettel', 'Raikkonen',
                  'Verstappen', 'Bottas', 'Ricciardo',
                  'Hulkenberg', 'Perez', 'Magnussen'
                  'Sainz', 'Alonso', 'Ocon', 'Leclerc',
                  'Grosjean', 'Gasly', 'Vandoorne',
                  'Ericsson', 'Stroll', 'Hartley', 'Sirotkin'],
                    
        'Points':[408, 320, 251, 249, 247, 170, 69, 62, 56,
                   53, 50, 49, 39, 37, 29, 12, 9, 6, 4, 1],
                     
        'Age':[33, 31, 39, 21, 29, 29, 31, 28, 26, 24, 37,
                      22, 21, 32, 22, 26, 28, 20, 29, 23]}
                        
# creating dataframe using DataFrame constructor
df = pd.DataFrame(dict1)
  
# Which row has maximum age | 
# who is the youngest driver ?
print(df[df.Age == df.Age.min()])

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.