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)
CodePudding user response:
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
CodePudding user response:
Given jch's example df:
A
0 a
1 b
2 c
3 d
4 e
5 f
6 g
7 h
8 i
9 j
Doing, let's drop 3 values, 'e' and the two values after it:
i = df.A.eq('e').idxmax()
df = df.drop(range(i, i 3))
print(df)
Output:
A
0 a
1 b
2 c
3 d
7 h
8 i
9 j