Select row with max and min value in Pandas dataframe

NumPy | Python Methods and Functions

Consider this dataset.

Output:

Using max on Dataframe —

Code # 1: shows the maximum of Driver, Points, Age columns.

# pandas and numpy imports

import pandas as pd

import numpy as np

 
# 2018 Driving World Championship data

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 ]}

  
# create a data frame using the DataFrame constructor

df = pd.DataFrame (dict1)

print (df. head ( 10 ))

# pandas and numpy imports

import pandas as pd

import numpy as np

 
# 2018 Driving World Championship data

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 ]}

  
# create a data frame using the DataFrame constructor

df = pd.DataFrame (dict1)

 
# the result shows a maximum of
# Driver, Points, Age columns.

print (df. max ())

Output:

Code # 2: Who scored maximum points

# pandas and numpy imports

import pandas as pd

import numpy as np

 
# 2018 Driving World Championship data

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 ]}

 
# create a data frame using the constructor DataFrame

df = pd.DataFrame ( dict1)

 
# Who scored more points?

print (df [df.Points = = df.Points. max ( )])

Output:

Code # 3: What is the maximum age

# pandas and numpy imports

import pandas as pd

import numpy as np

  
# 2018 Driving World Championship data

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 ]}

 
# create a data frame using the DataFrame constructor

df = pd.DataFrame (dict1)

 
# what is the maximum age?

print (df.Age. max ())

Output:

Code # 4: Which line has the maximum age in the data frame | who is the oldest driver?

# pandas and numpy imports

import pandas as pd

import numpy as np

 
# 2018 Driving World Championship data

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 ]}

 
# create a data frame using the DataFrame constructor

df = pd.DataFrame (dict1)

 
# Which one the row has a maximum age |
# who is the oldest driver?

print (df [df.Age = = df.Age. max ()])

Output:

Using min on Dataframe —

Code # 1: shows the minimum number of Driver columns, Points "," Age ".

# pandas and numpy imports

import pandas as pd

import numpy as np

<



Get Solution for free from DataCamp guru