pythonduplicatesduplication

Delete the entire row if the a value in value is equal to previous row in Python


I am new to python programming and I need a help to delete the entire row based on the value of a single column in a dataframe. I want to delete the row, if a value in a single column is equal to the previous row value.

The following is my data,

  x.id x.timestamp x.count
71    1  1435114605      61
72    1  1435114606      61
73    1  1435114659      61
74    1  1435114719      62
75    1  1435114726      62
76    1  1435114780      62
77    1  1435155998      62
78    1  1435156059      62
79    1  1435156076      62
80    1  1435156119      62

Here I want to delete the rows based on the x.x.count value.

My Output should be,

  x.id x.timestamp x.count
71    1  1435114605      61
74    1  1435114719      62

I can't use drop_duplicates function because the values would be reoccuring later in the column. I want to check the previous value and delete it.

Can anybody help me in doing this?

Thanks


Solution

  • If you don't want to just drop dupes:

    import pandas as pd
    
    df = df.groupby((df["x.count"] != df["x.count"].shift()).cumsum().values).first()
    

    Or:

    df = df.loc[df["x.count"].shift() != df["x.count"]]