Home »
Python »
Python Programs
Python - Return max value from pandas dataframe, not based on column or rows but as a whole
Given a Pandas DataFrame, we have to get the max value from it, not based on column or rows but as a whole.
By Pranit Sharma Last updated : September 26, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Sometimes, we are not interested in columns and rows and we just need a particular value as a whole, in this article, we are going to learn how to return the max value from pandas DataFrame which is not based on column or row but as a whole.
Returning max value from pandas dataframe
The df.to_numpy() is a method that will convert the DataFrame into the list of lists or we can say that it converts the DataFrame into an array, which means that all the rows of the Dataframe will be encapsulated in separate lists and together they will be encapsulated in another array.
The max() method is used to return the maximum values from a collection, series, or DataFrame column or row. We will first convert the DataFrame into a NumPy array and then we will find the max value.
Let us understand with the help of an example,
Python program to return max value from pandas dataframe
# Importing pandas package
import pandas as pd
# Importing numpy package
import numpy as np
# Creating dictionary
d = {
'Maths':[78,82,43,68,97],
'Physics':[87,28,34,86,79],
'Chemistry':[66,88,49,46,73]
}
# Creating DataFrame
df = pd.DataFrame(d)
# Display Original DataFrames
print("Created DataFrame:\n",df,"\n")
# Finding Max value
max = df.to_numpy().max()
# Display result
print("Result:\n",max)
Output:
Python Pandas Programs »