pandas 获取满足某些条件的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17215682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:56:17  来源:igfitidea点击:

Get indices that satisfy some criteria

pythonpandas

提问by Amelio Vazquez-Reina

In Pandas, how can I get a list of indices of a series/dataframe for the entries that satisfy some property?

在 Pandas 中,如何获取满足某些属性的条目的系列/数据框的索引列表?

The following returns a Seriesobject

下面返回一个Series对象

my_dataframe.loc[:,'some_column'] == 'some_value'

that looks like this:

看起来像这样:

519     True
509    False
826    False
503    False
511    False
512    False
500    False
507    False
516     True
504    False
521    False
510    False
351    False
522    False
526    False
517    False
501    False

but what I want is two lists, one with

但我想要的是两个列表,一个是

[519, 516]

and one with the rest of indices. How can I do this in Pandas?

和其他索引之一。我怎样才能在 Pandas 中做到这一点?

回答by Jeff

In [8]: df = DataFrame(randn(10,2),columns=list('AB'))

In [9]: df
Out[9]: 
          A         B
0 -1.046978  1.561624
1 -0.264645  0.717171
2  0.112354 -2.084449
3 -1.243482 -1.183749
4  1.055667  0.532444
5 -1.295805  2.168225
6 -1.239725  0.969934
7 -0.354017  1.434943
8 -0.867560  0.810315
9  0.097698 -0.033039

In [10]: df.loc[:,'B'] > 0
Out[10]: 
0     True
1     True
2    False
3    False
4     True
5     True
6     True
7     True
8     True
9    False
Name: B, dtype: bool

In [14]: x = df.loc[:,'B'] > 0

Per Tom/Andy, much simpler 

In [33]: x[x].index
Out[33]: Int64Index([0, 1, 4, 5, 6, 7, 8], dtype=int64)

In [34]: x[~x].index
Out[34]: Int64Index([2, 3, 9], dtype=int64)

回答by TomAugspurger

A slight variant on @Jeff's example:

@Jeff 示例的一个小变种:

In [18]: df
Out[18]: 
          A         B
0  0.319489  1.012319
1  0.494205 -0.918240
2  1.501922 -0.409661
3 -1.593702  0.705407
4 -0.735312  1.037567
5 -0.201132 -0.673124
6  1.237310 -0.877043
7 -0.946714  0.984164
8 -0.923548  0.415094
9  0.135281 -0.199951

In [14]  list1 =  df.index[df.loc[:, 'B'] > 0]

In [15]: list1
Out[15]: Int64Index([0, 3, 4, 7, 8], dtype=int64)

In [16]: list2 = df.index - list1

In [17]: list2
Out[17]: Int64Index([1, 2, 5, 6, 9], dtype=int64)