3

I have a Data-Frame like following:

A B C D
- - - -
h e l 0
t h i 0
i s m 0
q u e 1
s t i 1

I want to delete all the rows that have same value in D consecutively but leaving the first one. The result will be following:

A B C D
- - - - 
h e l 0
q u e 1

So, far I've done it using following code:

df[list(map(lambda x: (x == 0) or (df['D'][x] != df['D'][x-1]), range(len(D))))]

I'm wondering if there a better way to do this?

Lokesh
  • 2,842
  • 7
  • 32
  • 47

1 Answers1

4

Use -

df.loc[df['D'].shift(1) != df['D']]

Output

    A   B   C   D
0   h   e   l   0
3   q   u   e   1
Vivek Kalyanarangan
  • 8,951
  • 1
  • 23
  • 42