如何在特定标签之前/之前获取n个索引号?

时间:2019-06-21 18:44:36

标签: python arrays pandas list indexing

我有一个数据集,如下所示;我的目标是创建一个列表,其中包含Accepted之前的三个索引号。

i       Label         value
0       Rejected       12
1       Rejected       10
2       Rejected       22
3       Rejected       32
4       Rejected       25
5       Rejected       15
6       Accepted       42
7       Accepted       52
8       Accepted       17
9       Accepted       8
10      Accepted       10
11      Rejected       21
12      Rejected       32
13      Rejected       12

此示例的结果应为:

list = [3,4,5]

编辑: 我的数据集多次重复这种情况。在某些情况下,“接受范围”只能是两个样本,例如以下示例:

i       Label         value
0       Rejected       12
1       Rejected       10
2       Rejected       22
3       Rejected       32
4       Rejected       25
5       Rejected       15
6       Accepted       42
7       Accepted       52
8       Rejected       17
9       Rejected       8
10      Rejected       10
11      Rejected       21
12      Rejected       32
13      Rejected       12

2 个答案:

答案 0 :(得分:4)

您可以使用:

df[df.Label.ne('Accepted')&df.Label.shift(-3).eq('Accepted')].index

Int64Index([3, 4, 5], dtype='int64', name='i')

答案 1 :(得分:2)

您可以使用.loc:

index = df.loc[df['Label']=='Accepted', 'Label'].first_valid_index()
[index - i for i in range(1, 4)]
>> [5, 4, 3]