Is there a pandas function to delete rows by comparing two values

Question:

I have a DataFrame which has a column with increasing numbers, representing the seconds of a day like this:

Index SecOfDay
1 0
2 10
3 21
4 23
5 31

Now I want to delete every row of the DataFrame, where the difference of secOfDay to the column before is smaller than 10. To have the DataFrame look like this:

Index SecOfDay
1 0
2 10
3 21
4 31

The only way to solve this was for me was a loop. Is there a way to do this more elegant in pandas maybe?

Thanks for your help!

Asked By: zuhausebeidermann

||

Answers:

Example

df = pd.DataFrame([0, 10, 21, 23, 26, 31, 43, 48, 61], columns=['sec'])

df

    sec
0   0
1   10
2   21
3   23
4   26
5   31
6   43
7   48
8   61

Code

result = [df['sec'].iloc[0]]
for i in df['sec']:
    if i - result[-1] >= 10:
        result.append(i)

result :

[0, 10, 21, 31, 43, 61]

make result to dataframe

pd.DataFrame(result, columns=['sec'])

output

    sec
0   0
1   10
2   21
3   31
4   43
5   61

I could have made it without loop, but it was much messier.
I will appreciate any other opinions

Answered By: Panda Kim