- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想检索与包含 n 个最大值的列集相对应的 n 个列名。然后,仅当值高于给定阈值时,这些名称才会保留在列表中。
举个例子,给定三列“Paul”、“Eric”、“Marc”,假设我最多需要 2 个列名,阈值为 0.5。然后我将获得“最佳”列,如下所示:
import pandas as pd
import numpy as np
start = '2020-01-01 00:00+00:00'
end = '2020-01-01 05:00+00:00'
pr1h = pd.period_range(start=start, end=end, freq='1h')
r = len(pr1h)
df = pd.DataFrame(np.random.rand(r,3), index=pr1h, columns=['Paul', 'Marc', 'Eric'])
处理后:
df
Paul Marc Eric Bests
2020-01-01 00:00 0.124974 0.525182 0.415339 ['Marc']
2020-01-01 01:00 0.991917 0.489479 0.668359 ['Paul', 'Eric']
2020-01-01 02:00 0.204156 0.610034 0.644715 ['Eric', 'Marc']
2020-01-01 03:00 0.385546 0.981641 0.089667 ['Marc']
2020-01-01 04:00 0.912330 0.711822 0.148064 ['Paul', 'Marc']
2020-01-01 05:00 0.301186 0.313572 0.323487 []
我可以在 this question/answer 上找到这显示了一种根据给定行中值的排名获取索引的方法。我想这可能是一个起点(可能在速度方面没有优化,因为运行了几次,但这似乎是一个好的开始。
然后我可以:
df1['1st_largest'] = df.columns[df.values.argsort(1)[:,-1]]
df2['2nd_largest'] = df.columns[df.values.argsort(1)[:,-2]]
我的数组不应超过 20 到 50 列,因此我保留使用 argsort
而不是 argpartition
。
但现在,我陷入了困境。我不知道如何检查与这些列之一相关的值是否高于 0.5,以便我可以将其放入列表中。
欢迎任何帮助,谢谢!
最佳答案
一种方法是使用 where
和 stack
屏蔽数据帧:
df['Bests'] = (df.where(df.gt(0.5)) # change 0.5 to your threshold
.stack().groupby(level=0)
.apply(lambda x: x.nlargest(2).index
.get_level_values(1).to_list()
)
)
输出:
Paul Marc Eric Bests
2020-01-01 00:00 0.124974 0.525182 0.415339 [Marc]
2020-01-01 01:00 0.991917 0.489479 0.668359 [Paul, Eric]
2020-01-01 02:00 0.204156 0.610034 0.644715 [Eric, Marc]
2020-01-01 03:00 0.385546 0.981641 0.089667 [Marc]
2020-01-01 04:00 0.912330 0.711822 0.148064 [Paul, Marc]
2020-01-01 05:00 0.301186 0.313572 0.323487 NaN
关于Python/ Pandas : Finding index for the nlargest and keeping only those above a value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61962975/
我有一个 合作伙伴集合,我正在使用 pymongo 来检索数据 当我使用 MongoDB 查询集合时,我看到以下结果 db.partner.find({'unique_key': 'c89dbe313
嗨,我正在尝试在一个 find 命令中查找所有 js 和 css 文件。我尝试了以下所有方法但徒劳无功: find WebContent -name "*.[jc]ss?" find WebConte
我使用以下 find 命令查找并显示所有具有输入文本模式的文件。 找 。 -type f -print|xargs grep -n "模式" 我有很多项目文件夹,每个文件夹都有自己的名为“Makefi
我在Windows环境中使用Gnuwin32二进制文件。 当我想查找某种类型的文件时(例如PDF),我通常运行: find . -iname '*.pdf' -print 这在任何UNIX系统上均可完
我使用的是 Julia 编程语言,我知道你可以通过以下方式使用 find 函数: a = [ 1 2 3 4 3 5 3 6 7 8 9 3 ] find(a .== 3) 它将返回:3,5,7,12
jsperf's link 我不是 jQuery 专家(甚至不是一个好的用户),我没有研究它的整个源代码(只有一小部分不能帮助我解决这个问题)。 有人可以为我解释一下吗? 最佳答案 这个: $p.fi
我应该如何在 CentOS 7 中修复这个错误? [jalal@goku HW4]$ git clone https://github.com/pathak22/pyflow.git Cloning
是否可以更改传递给 find 中的 exec 的参数?例如,我需要以不同的名称复制文件:*.txt -> *.new.txt现在我正在为两个命令执行此操作: find /root/test -name
我想通过cleartool find 命令找到*.cs 和*.cpp 文件。但它失败了。 cleartool find "M:\test_view\code" -name "*.cs *.cpp"
我正在使用 PyMongo,看到有人建议使用 find()[:] 而不是 find()。很好奇有什么区别? 最佳答案 [:] 制作列表的浅拷贝,因此对对象的引用是相同的。我查看了 Pymongo 文档
我正在处理文件和目录,以在每个目录中查找最近修改的文件。我的代码可以工作,但作为 Ruby 的新手,我无法正确处理错误。 我使用 Find.find 获取递归目录列表,为每个目录调用我自己的函数 ne
/usr/bin/ld: cannot find -ldlib /usr/bin/ld: cannot find -lcblas /usr/bin/ld: cannot find -llapack 在
我有一些数据文件的一系列索引文件,它们基本上采用这种格式 索引文件:asdfg.log.1234.2345.index 数据文件:asdfg.log 这个想法是搜索所有索引文件。如果值 XXXX 出现
我有一个 find我运行以查找名称包含 foo 的文件的命令. 我想跳过 .git目录。下面的命令有效 除了 它打印一个 烦人 .git任何时候它跳过 .git目录: find . ( -name .
我有以下想做的事情: find . -maxdepth 6 \( -name \*.tar.gz -o -name bediskmodel -o -name src -o -name ciao -o
当我在表中查找隐藏字段时,我看到了两个隐藏字段。但是,我想通过 ID 进一步细化这两个字段。我注意到,当我使用“包含”在整个表上使用 find 时,我得到了 2 个字段。但是,如果我对隐藏字段的查找结
我正在使用下面的命令生成文件列表及其 m5sum。问题是某些文件或文件夹的名称中有空格。我将如何处理这些? find -type f -name \* | xargs md5sum 最佳答案 尝试:
我正在使用下面的命令生成文件列表及其 m5sum。问题是某些文件或文件夹的名称中有空格。我将如何处理这些? find -type f -name \* | xargs md5sum 最佳答案 尝试:
我有一个使用正则表达式查找文件的脚本。代码如下: find $dir | grep "$regex" 脚本运行有点慢,我想优化一下。搜索需要一些时间来执行,我想从中获得更好的性能。我试过这种尝试: f
这令人沮丧。我认为问题出在 api 响应返回的对象上。也许它是在字符串中,所以我所做的就是复制“postman”的响应并将其直接粘贴到js上。这样我就可以确定它在对象/数组中。但结果还是同样的错误。
我是一名优秀的程序员,十分优秀!