Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: delete consecutive duplicates but keep the first and last value

Tags:

python

pandas

So I have a set of values in a column that looks like this:

1 0 2 1 1 0 0 0 0 0 1 2 0 0 0 0 4 

I'm trying to delete the repeating zeros but keep the first and last ones. End result should look like this:

1 0 2 1 1 0 0 1 2 0 0 4

Drop duplicates won't work because it deletes all the zeros, not independent consecutive zeros.

df = df.loc[df.people.shift() != df.people]

this works well but does not save the last number of that consecutive one

enter image description here

like image 281
bobo T Avatar asked Oct 21 '25 19:10

bobo T


1 Answers

Using fillna with limit

s[s.replace(0,np.nan).ffill(limit=1).bfill(limit=1).notnull()]
Out[387]: 
0     1
1     0
2     2
3     1
4     1
5     0
9     0
10    1
11    2
12    0
15    0
16    4
dtype: int64
like image 53
BENY Avatar answered Oct 24 '25 10:10

BENY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!