Python 从熊猫数据帧整体返回最大值,而不是基于列或行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24571005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:47:41  来源:igfitidea点击:

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

pythonpandasmaxdataframe

提问by Boosted_d16

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.

我假设 df.max() 会完成这项工作,但它为每列返回一个最大值,但我对此不感兴趣。我需要整个数据帧的最大值。

采纳答案by unutbu

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

可以使用 获得 DataFrame 中所有值的最大值df.to_numpy().max(),或者pandas < 0.24.0我们使用df.values.max()

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

The max is frather than 43.0 since, in CPython2,

最大值f不是 43.0,因为在 CPython2 中,

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

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

在 CPython2 中,不同类型的对象......按它们的类型名称排序。所以 anystr比 any 更大,int因为'str' > 'int'.

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

在 Python3 中,字符串和整数的比较会产生一个TypeError.



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

要仅在数字列中查找最大值,请使用

df.select_dtypes(include=[np.number]).max()

回答by ntg

For the max, check the previous answer... For the max of the values use e.g.:

对于最大值,请检查上一个答案...对于值的最大值,请使用例如:

val_cols = [c for c in df.columns if c.startswith('val')]
print df[val_cols].max()

回答by aileronajay

Max can be found in these two steps:

Max可以在这两个步骤中找到:

maxForRow = allData.max(axis=0) #max for each row
globalMax = maxForRow.max(); #max across all rows

回答by Rilwan Adewoyin

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.

说明:
series = df.max()给你一个包含每列最大值的系列。
因此,series.max()为您提供整个数据帧的最大值。

:) best answers are usually the simplest

:) 最好的答案通常是最简单的