Python 获取满足条件的 Pandas DataFrame 行的整数索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18199288/
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
Getting the integer index of a Pandas DataFrame row fulfilling a condition?
提问by Dun Peal
I have the following DataFrame:
我有以下数据帧:
a b c
b
2 1 2 3
5 4 5 6
As you can see, column b
is used as an index. I want to get the ordinal number of the row fulfilling ('b' == 5)
, which in this case would be 1
.
如您所见,列b
用作索引。我想获得满足的行的序号('b' == 5)
,在这种情况下是1
.
The column being tested can be either an index column (as with b
in this case) or a regular column, e.g. I may want to find the index of the row fulfilling ('c' == 6)
.
被测试的列可以是索引列(b
在这种情况下)或常规列,例如,我可能想找到满足('c' == 6)
.
采纳答案by unutbu
You could use np.wherelike this:
你可以像这样使用np.where:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(1,7).reshape(2,3),
columns = list('abc'),
index=pd.Series([2,5], name='b'))
print(df)
# a b c
# b
# 2 1 2 3
# 5 4 5 6
print(np.where(df.index==5)[0])
# [1]
print(np.where(df['c']==6)[0])
# [1]
The value returned is an array since there could be more than one row with a particular index or value in a column.
返回的值是一个数组,因为一列中可能有多个具有特定索引或值的行。
回答by hlin117
Use Index.get_locinstead.
Reusing @unutbu's set up code, you'll achieve the same results.
重用@unutbu 的设置代码,您将获得相同的结果。
>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame(np.arange(1,7).reshape(2,3),
columns = list('abc'),
index=pd.Series([2,5], name='b'))
>>> df
a b c
b
2 1 2 3
5 4 5 6
>>> df.index.get_loc(5)
1
回答by Gabriele Picco
With Index.get_locand general condition:
使用Index.get_loc和一般条件:
>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame(np.arange(1,7).reshape(2,3),
columns = list('abc'),
index=pd.Series([2,5], name='b'))
>>> df
a b c
b
2 1 2 3
5 4 5 6
>>> df.index.get_loc(df.index[df['b'] == 5][0])
1