Mohd Bilal
Mohd Bilal

Reputation: 101

Keep only first occurrence of continuous repetition of values in a dataframe

I have a dataframe like:

time      value

t1         50
t2         200
t3         200
t4         130
t5         78
t6         200
t7         19
t8         200
t9         200
t10        200
t11        59

and in the result, I want to keep rows having value = 200. If it comes multiple times in continuation then keep only the first occurrence of value 200.

So the desired output dataframe should be:

time      value

t2         200
t6         200
t8         200

Please help me to solve this problem.

Thank you

Upvotes: 0

Views: 131

Answers (1)

Arkadiusz
Arkadiusz

Reputation: 1875

It can be solved this way:

df.loc[(df['value'] == 200) & (df['value'].shift(1) != 200)].reset_index(drop=True)

From the dataframe we take everything what equals 200 and doesn't have 200 above. You can also add 'reset_index' at the end if you want to see index values starting from 0.

Output:

    time    value
  0   t2      200
  1   t6      200
  2   t8      200

Upvotes: 1

Related Questions