way to drop values starting at a given index in pandas

Viewed 32

This code works but seems so hairy. Is there a better way to drop 100 rows from a dataframe starting from the row where a certain value criteria is met?

In my case, I want to find next row where a value in column_name is < 21000, then drop that and the next 100 rows in the dataframe.

pd.drop(pd[(pd.index >= pd.loc[pd[column_name] < 21000].index[0])][:100].index, inplace=True)

The index is timedate values.

2 Answers

Given jch's example df, plus a datetime index:

            A
2021-01-01  a
2021-01-04  b
2021-01-07  c
2021-01-10  d
2021-01-13  e
2021-01-16  f
2021-01-19  g
2021-01-22  h
2021-01-25  i
2021-01-28  j

Doing, let's drop 3 values, 'e' and the two values after it:

i = df.A.eq('e').argmax()
df = df.drop(df.index[i:i+3])
print(df)

Output:

            A
2021-01-01  a
2021-01-04  b
2021-01-07  c
2021-01-10  d
2021-01-22  h
2021-01-25  i
2021-01-28  j

Thinking of it from the other direction, you could just include everything around the 100. The example below does that, but 'drops' 3 instead of 100.

df = pd.DataFrame({'A':list('abcdefghij')})
print(df) 


   A
0  a
1  b
2  c
3  d
4  e
5  f
6  g
7  h
8  i
9  j

Execute

r = df['A'].eq('d').argmax()
pd.concat([df.iloc[:r],df.iloc[r+3:]])

Result

   A
0  a
1  b
2  c
6  g
7  h
8  i
9  j
Related