python - Pandas DataFrame - delete rows that have same value at a particular column as a previous row -
I have a panda dataframe, I want to check for each row if it has the same value (call it in a particular column This porduct_type), and if it does, remove it. In other words, from a group of rows continuously with the same value on a particular column, I want to keep only one.
Example, if column A is the one we do not want to duplicate constantly:
input = ab 0 1 1 2 2 2 1 1 2 2 20 0 11 100 5 2 200 Output = AB 0 1 1 2 1 10 0 11 100 5 2 200
< P> It is a bit tricky, but you can do something like & gt; & Gt; & Gt; Df.groupby ((df ["a"]! = Df ["a"]. Shift ()). Cumsum (). Value). First () ABC 1 0 1 1 2 2 1 10 3 11 11 100 4 5 2 200
Comments
Post a Comment