本文翻译自:Use a list of values to select rows from a pandas dataframe [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
Lets say I have the following pandas dataframe: 假设我有以下pandas数据帧:
df = DataFrame({'A' : [5,6,3,4], 'B' : [1,2,3, 5]})
df
A B
0 5 1
1 6 2
2 3 3
3 4 5
I can subset based on a specific value: 我可以根据特定值进行子集化:
x = df[df['A'] == 3]
x
A B
2 3 3
But how can I subset based on a list of values? 但是我如何根据值列表进行子集化? - something like this: - 这样的事情:
list_of_values = [3,6]
y = df[df['A'] in list_of_values]
#1楼
参考:https://stackoom.com/question/okmq/使用值列表从pandas数据框中选择行-duplicate
#2楼
This is indeed a duplicate of how to filter the dataframe rows of pandas by "within"/"in"? 这确实是如何通过“within”/“in”过滤pandas的数据帧行的重复? , translating the response to your example gives: ,将响应翻译成你的例子给出:
In [5]: df = DataFrame({'A' : [5,6,3,4], 'B' : [1,2,3, 5]})
In [6]: df
Out[6]:
A B
0 5 1
1 6 2
2 3 3
3 4 5
In [7]: df[df['A'].isin([3, 6])]
Out[7]:
A B
1 6 2
2 3 3