[英]How to select rows in pandas based on list of values
I'm trying to find out a way how I can select rows in pandas dataframe based that some values will be in my list. 我试图找出一种方法,我可以在pandas数据框中选择行,因为某些值将在我的列表中。 For example
例如
df = pd.DataFrame(np.arange(6).reshape(3,2), columns=['A','B'])
A B
0 0 1
1 2 3
2 4 5
I know that I can select certain row, eg 我知道我可以选择某一行,例如
df[df.A==0]
will select me row with A=0. 将选择A = 0的行。 What I want is to select multiple rows whose values will be in my list, eg A in [0,2].
我想要的是选择多个行,其值将在我的列表中,例如[0,2]中的A. I tried
我试过了
df[df.A in [0,2]]
df[list(df.A)==[0,2]]
but nothing works. 但没有任何作用。 In R language I can provide %in% operator.
在R语言中,我可以提供%in%运算符。 In python syntax we can use A in [0,2], etc. How I can select subset of rows in pandas in this case?
在python语法中我们可以在[0,2]中使用A等。在这种情况下,如何在pandas中选择行的子集? Thanks, Valentin.
谢谢,瓦伦丁。
pd.isin() will select multiple values: pd.isin()将选择多个值:
>>> df[df.A.isin([0,2])]
A B
0 0 1
1 2 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.