Warm tip: This article is reproduced from serverfault.com, please click

Pandas once condition is met in column delete n rows

发布于 2020-11-28 01:04:27

I have a DataFrame that looks like df = pd.DataFrame({'col1': [.8,.9,1,1,1,.9,1,.9,.8]}).

The goal I have is once a number 1 in 'col1' is found, remove the next five rows.

Example

    col1
0   0.8
1   0.9
2   1.0
3   1.0
4   1.0
5   0.9
6   1.0
7   0.9
8   0.8

Expected Output

    col1
0   0.8
1   0.9
2   1.0
3   0.8

Any ideas?

Questioner
Andrew Horowitz
Viewed
0
sammywemmy 2020-11-28 13:01:25

You could use numpy.r_ to generate the integers:

position_of_1 = np.argmax(df.col1.eq(1)) # df.col1.eq(1).idxmax() not fool-proof

integers = np.r_[: position_of_1 + 1, 
                 range(position_of_1 + 6, len(df))
                 ]

df.iloc[integers]


col1
0   0.8
1   0.9
2   1.0
8   0.8

Thanks to @Ben, for the suggestion on np.argmax; it would be much better/safer to use np.argmax, for scenarios where the index are not numbers or not in proper form: