有效地选择与Pandas DataFrame中的几个值之一匹配的行 [英] Efficiently select rows that match one of several values in Pandas DataFrame
本文介绍了有效地选择与Pandas DataFrame中的几个值之一匹配的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Pandas DataFrame中提供数据,如下所示:
Given data in a Pandas DataFrame like the following:
Name Amount
---------------
Alice 100
Bob 50
Charlie 200
Alice 30
Charlie 10
我想选择Name
是集合{Alice, Bob}
Name Amount
---------------
Alice 100
Bob 50
Alice 30
问题
在Pandas中执行此操作的有效方法是什么?
Question
What is an efficient way to do this in Pandas?
我看到的选项
- 遍历行,使用Python处理逻辑
-
选择并合并许多类似于以下内容的语句
- Loop through rows, handling the logic with Python
Select and merge many statements like the following
merge(df[df.name = specific_name] for specific_name in names) # something like this
执行某种联接
Perform some sort of join
这里的性能折衷是什么?一种解决方案何时比其他解决方案更好?我缺少什么解决方案?
What are the performance trade-offs here? When is one solution better than the others? What solutions am I missing?
虽然上面的示例使用字符串,但我的实际工作是使用数百万行中10-100个整数的匹配项,因此快速的NumPy操作可能很重要.
While the example above uses strings my actual job uses matches on 10-100 integers over millions of rows and so fast NumPy operations may be relevant.
推荐答案
您可以使用 查看全文