Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return max value from pandas dataframe as a whole, not based on column or rows

I am trying to get the max value from a panda dataframe as whole. I am not interested in what row or column it came from. I am just interested in a single max value within the DataFrame.

Here is my DataFrame:

df = pd.DataFrame({'group1': ['a','a','a','b','b','b','c','c','d','d','d','d','d'],                         'group2': ['c','c','d','d','d','e','f','f','e','d','d','d','e'],                         'value1': [1.1,2,3,4,5,6,7,8,9,1,2,3,4],                         'value2': [7.1,8,9,10,11,12,43,12,34,5,6,2,3]}) 

This is what it looks like:

   group1 group2  value1  value2 0       a      c     1.1     7.1 1       a      c     2.0     8.0 2       a      d     3.0     9.0 3       b      d     4.0    10.0 4       b      d     5.0    11.0 5       b      e     6.0    12.0 6       c      f     7.0    43.0 7       c      f     8.0    12.0 8       d      e     9.0    34.0 9       d      d     1.0     5.0 10      d      d     2.0     6.0 11      d      d     3.0     2.0 12      d      e     4.0     3.0 

Expected output:

43.0 

I was under the assumption that df.max() would do this job but it returns a max value for each column but I am not interested in that. I need the max from an entire dataframe.

like image 288
Boosted_d16 Avatar asked Jul 04 '14 09:07

Boosted_d16


People also ask

How do you find the maximum value in an entire data frame?

To find the maximum value of a Pandas DataFrame, you can use pandas. DataFrame. max() method. Using max(), you can find the maximum value along an axis: row wise or column wise, or maximum of the entire DataFrame.

How do you find the maximum value of A pandas DataFrame column in python?

To find the maximum value of a column and to return its corresponding row values in Pandas, we can use df. loc[df[col]. idxmax()].

How do you get max rows in pandas?

Find Maximum Element in Pandas DataFrame's RowIf the axis equals to 0, the max() method will find the max element of each column. On the other hand, if the axis equals to 1, the max() will find the max element of each row.

How do you find the highest value in a row and return column header in python?

To create the new column 'Max', use df['Max'] = df. idxmax(axis=1) . To find the row index at which the maximum value occurs in each column, use df. idxmax() (or equivalently df.


2 Answers

The max of all the values in the DataFrame can be obtained using df.to_numpy().max(), or for pandas < 0.24.0 we use df.values.max():

In [10]: df.to_numpy().max() Out[10]: 'f' 

The max is f rather than 43.0 since, in CPython2,

In [11]: 'f' > 43.0 Out[11]: True 

In CPython2, Objects of different types ... are ordered by their type names. So any str compares as greater than any int since 'str' > 'int'.

In Python3, comparison of strings and ints raises a TypeError.


To find the max value in the numeric columns only, use

df.select_dtypes(include=[np.number]).max() 
like image 88
unutbu Avatar answered Oct 20 '22 18:10

unutbu


Hi the simplest answer is the following. Answer:

df.max().max() 

Explanation:
series = df.max() give you a Series containing the maximum values for each column.
Therefore series.max()gives you the maximum for the whole dataframe.

:) best answers are usually the simplest

like image 37
Rilwan Adewoyin Avatar answered Oct 20 '22 18:10

Rilwan Adewoyin