Olga B
Olga B

Reputation: 3

How to delete rows following after in dataframe python

Please help. How can I delete rows following after the row where first meets 1 in limits of one ID ? I have a dataframe with 2 level indexes.

What I have

This is how I want to delete

I tried this

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])

But it's an error: KeyError: 2015

It's also a problem when I tried

example_i.loc[4,2014]

as there is no such row.

Upvotes: 0

Views: 89

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 149155

I cannot imagine a way to cleanly vectorize the operation. But it is easy to scan the dataframe in order to build a list of the lines to drop. So the code could be:

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 should now be [(1, 2015), (1, 2016), (2, 2016), (4, 2015), (4, 2016), (5, 2015), (5, 2016)]

So this is enough to drop the lines:

example_i.drop(to_drop, inplace=True)

and we have as expected:

         default
id time         
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

Upvotes: 1

Related Questions