如何在dataframe python中删除后面的行

如何在dataframe python中删除后面的行,python,pandas,dataframe,Python,Pandas,Dataframe,请帮忙。在一个ID的限制中,如何删除第一个满足1的行之后的行?我有一个带有2级索引的数据帧 我试过这个 for i in range(1,6): for j in range(2013,2016): if example_i.loc[i,j]['default']>0: example_i.drop(index=[i,j+1]) 但这是一个错误:关键错误:2015年 当我尝试时,这也是一个问题 example_i.loc[4,2014]

请帮忙。在一个ID的限制中,如何删除第一个满足1的行之后的行?我有一个带有2级索引的数据帧

我试过这个

for i in range(1,6):
    for j in range(2013,2016):
        if example_i.loc[i,j]['default']>0:
            example_i.drop(index=[i,j+1])
但这是一个错误:关键错误:2015年

当我尝试时,这也是一个问题

example_i.loc[4,2014]

因为没有这样的行。

我无法想象一种干净地矢量化操作的方法。但是扫描数据帧以建立要删除的行的列表很容易。因此,代码可以是:

cur=0

to_drop = []
for row in example_i.itertuples():
    if cur != row[0][0]:
        cur = row[0][0]
        drop = False
    if drop:
        to_drop.append(row[0])
    if row[1] == 1:
        drop = True
to_drop
现在应该是
[(1,2015),(1,2016),(2,2016),(4,2015),(4,2016),(5,2015),(5,2016)]

因此,这就足以让我们放下这一行:

example_i.drop(to_drop, inplace=True)
正如我们所料:

默认值
身份证时间
1  2013        0
2014        1
2  2013        0
2014        0
2015        1
3  2013        0
2014        0
2015        0
2016        0
4  2013        1
5  2013        0
2014        1