简体   繁体   中英

Delete Consecutive Values of Specific Number - Python Dataframe

How can you remove consecutive duplicates of a specific value?

I am aware of the groupby() function but that deletes consecutive duplicates of any value.

See the example code below. The specific value is 2, in which I want to remove duplicates

import pandas as pd
from itertools import groupby

example = [1,1,5,2,2,2,7,9,9,2,2]
Col1 = pd.DataFrame(res)
# This does not work for just a specific number
res = [i[0] for i in groupby(Col1)] 

The resulting DataFrame would be [1,1,5,2,7,9,9,2]

Doing this with pandas seems overkill unless you are using pandas for other purposes, eg:

In []:
import itertools as it
example = [1,1,5,2,2,2,7,9,9,2,2]
[x for k, g in it.groupby(example) for x in ([k] if k == 2 else g)]

Out[]:
[1, 1, 5, 2, 7, 9, 9, 2]

Try using the column's diff being equal to 0.

In your case, where we only care about deduplication when the value of the column is 2, we condition on the diff being nonzero or the column being not equal to 2:

import pandas as pd

example = [1,1,5,2,2,2,7,9,9,2,2]

df = pd.DataFrame(dict(a=example))
df.loc[(df.a.diff() != 0) | (df.a != 2)]

How can you remove consecutive duplicates of a specific value?

I am aware of the groupby() function but that deletes consecutive duplicates of any value.

See the example code below. The specific value is 2, in which I want to remove duplicates

import pandas as pd
from itertools import groupby

example = [1,1,5,2,2,2,7,9,9,2,2]
Col1 = pd.DataFrame(res)
# This does not work for just a specific number
res = [i[0] for i in groupby(Col1)] 

The resulting DataFrame would be [1,1,5,2,7,9,9,2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM