python - 数据框中的 n 个最高值

标签 python pandas max

我有一个像这样的 Pandas 数据框:

        column0     column1     column2     column3     column4
row0    179319.0    180895.0    94962.0     130734.0    0
row1    89659.5     90447.5     47481.0     65367.0     0
row2    59773.0     60298.33333 31654.0     43578.0     0
row3    44829.75    45223.75    23740.5     32683.5     0
row4    35863.8     36179.0     18992.4     26146.8     0
row5    29886.5     30149.16666 15827.0     21789.0     0
row6    25617.0     25842.14285 13566.0     18676.28571 0
row7    22414.875   22611.875   11870.25    16341.75    0
row8    19924.33333 20099.44444 10551.33333 14526.0     0

我想得到一些像 9(行数)最高值的索引,或者像每列的最高值的计数,比如:

column0  column1  column2  column3  column4
3        3        1        2        0

在我的示例中,9 个最高值将是来自第 0 行的 column0、column1、column2 和 column3 的值,来自 row1 的 column0、column1 和 column3 的值,以及来自 row2 的 column0 和 column1 的值。

有什么想法吗?谢谢!

最佳答案

堆栈 之后的 IIUC nlargest

df.stack().nlargest(9).groupby(level=1).count().reindex(df.columns,fill_value=0)
Out[48]: 
column0    3
column1    3
column2    1
column3    2
column4    0
dtype: int64

关于python - 数据框中的 n 个最高值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55766531/

相关文章:

mysql - 使用计数值作为偏移量的 SQL

python ORM 允许创建表和批量插入吗?

python - 快速加载和处理9000万个元素的字典

python - 修改当前 GET 请求中的查询参数以获取新的 url

python - 使用 Pandas 选择、切片和聚合时态数据

python - 使用 Dask 读取时如何按日期时间列过滤 Parquet 文件?

sql-server - SQL Server 2008 MAX 和 MIN 函数在处理负数时无法按预期工作

python - 将带转义的 python 正则表达式分成多行

python - 根据文本将值从一个数据帧平均分配到另一个数据帧

c# - 使用 C# 在数据表中查找最小值和最大值