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!
CodePudding user response:
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
series = df['sec'].tolist()
result = [series[0]]
for i in a:
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