Thursday, July 4, 2024
HomeLanguagesPythonHow to Get the maximum value from the Pandas dataframe in Python?

How to Get the maximum value from the Pandas dataframe in Python?

Python Pandas max() function returns the maximum of the values over the requested axis.

Syntax: dataframe.max(axis)

where,

  • axis=0 specifies column
  • axis=1 specifies row

Example 1: Get maximum value in dataframe row

To get the maximum value in a dataframe row simply call the max() function with axis set to 1.

Syntax: dataframe.max(axis=1)

Python3




# import pandas module
import pandas as pd
  
# create a dataframe
# with 5 rows and 4 columns
data = pd.DataFrame({
    'name': ['sravan', 'ojsawi', 'bobby'
             'rohith', 'gnanesh'],
    'subjects': ['java', 'php', 'html/css',
                 'python', 'R'],
    'marks': [98, 90, 78, 91, 87],
    'age': [11, 23, 23, 21, 21]
})
  
# display dataframe
print(data)
  
# get the maximum in row
data.max(axis=1)


Output:

Example 2: Get the maximum value in column

To get the maximum value in a column simply call the max() function using the axis set to 0.

Syntax: dataframe.max(axis=0)

Python3




# import pandas module
import pandas as pd
  
# create a dataframe
# with 5 rows and 4 columns
data = pd.DataFrame({
    'name': ['sravan', 'ojsawi', 'bobby'
             'rohith', 'gnanesh'],
    'subjects': ['java', 'php', 'html/css',
                 'python', 'R'],
    'marks': [98, 90, 78, 91, 87],
    'age': [11, 23, 23, 21, 21]
})
  
# display dataframe
print(data)
  
# get the maximum in column
data.max(axis=0)


Output:

Example 3: Get the maximum value in a particular column

To get the maximum value in a particular column call the dataframe with the specific column name and max() function.

Syntax: dataframe[‘column_name’].max()

Python3




# import pandas module
import pandas as pd
  
# create a dataframe
# with 5 rows and 4 columns
data = pd.DataFrame({
    'name': ['sravan', 'ojsawi', 'bobby'
             'rohith', 'gnanesh'],
    'subjects': ['java', 'php', 'html/css'
                 'python', 'R'],
    'marks': [98, 90, 78, 91, 87],
    'age': [11, 23, 23, 21, 21]
})
  
# display dataframe
print(data)
  
# get the max in name  column
print(data['name'].max())
  
# get the max in subjects  column
print(data['subjects'].max())
  
# get the max in age  column
print(data['age'].max())
  
# get the max in marks  column
print(data['marks'].max())


Output:

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments