Skip to content Skip to sidebar Skip to footer

Removing Values That Repeat More Than 5 Times In Pandas Dataframe

I am using pandas to work with csv files. I need to remove a few repeated values if they occur consecutively. I understand there is a duplicate function that removes any value th

Solution 1:

This should do it:

>> df = pd.Series([1,1,3,1,1,1,1,1,2])
>> df.groupby((df.shift() != df).cumsum())\
     .filter(lambdax: len(x) < 5)
01112382

Solution 2:

Showing how answer by elyase also works for DataFrame (not Series).

>> df = pd.DataFrame(np.array([[1,1,3,1,1,1,1,1,2]]).transpose(),columns = ["col"])
>> df.groupby((df["col"].shift() != df["col"]).cumsum()).filter(lambda x: len(x) < 5)
    col
 01112382

Post a Comment for "Removing Values That Repeat More Than 5 Times In Pandas Dataframe"