假设我在CSV文件中有以下匹配数据:
name,match1,match2,match3
Alice,2,4,3
Bob,2,3,4
Charlie,1,0,4
我正在写一个python程序。在我的程序的某个地方,我收集了一个存储在列表中的匹配的分数,比如x = [1,0,4]
。我用熊猫找到了这些分数在数据中的位置,我可以打印“发现”或“未发现”。但是,我希望我的代码打印出这些分数对应的name
。在这种情况下,程序应该输出“charlie”,因为charlie具有所有这些值[1,0,4]
。我该怎么做
我将有一个大的数据集,所以我必须能够告诉哪个名称对应于我传递给程序的数字
是的,以下是如何比较数据帧中的整行:
此外,如果您在读取CSV时直接将
name
设置为索引列,则会使生活变得最简单现在您可以看到,执行
df == x
或等效的df.eq(x)
并不是您想要的,因为它执行元素级比较并返回一行True/False。因此,您需要使用.all(axis=1)
来聚合这些行,这将发现所有比较结果都是True
的行…最后,因为您只需要这些行的
name
:这正是你想要的。(为了清晰起见,我只在表达式中添加了空格)
您需要使用DataFrame.loc,其工作原理如下:
也许可以试试这样:
相关问题 更多 >
编程相关推荐