从大 pandas 数据帧返回最大值,而不是基于列或行 [英] return max value from panda 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.
这是我的数据框:
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]})
这是它的样子:
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
预期输出:
43.0
我假设df.max()将执行此作业,但它返回一个最大值每一列,但我不感兴趣。我需要整个数据帧的最大值。
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.
推荐答案
DataFrame中所有值的最大值可以使用 df.values.max )
:
The max of all the values in the DataFrame can be obtained using df.values.max()
:
In [10]: df.values.max()
Out[10]: 'f'
最大为 f
而不是43.0,因为在CPython2中,
The max is f
rather than 43.0 since, in CPython2,
In [11]: 'f' > 43.0
Out[11]: True
在CPython2中,不同类型的对象...按照类型名称 排序
。因此, str
比 int
更大,因为'str'> 'int'
。
In CPython2, Objects of different types ... are
ordered by their type names. So any str
compares as greater than any int
since 'str' > 'int'
.
在Python3中,string和int的比较引发了一个TypeError。
In Python3, comparison of strings and ints raises a TypeError.
这篇关于从大 pandas 数据帧返回最大值,而不是基于列或行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!