简体   繁体   中英

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 xxcount 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

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"]]

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