Drop rows before a column value changes the first time

Viewed 32

I have thousands of pd.Dataframes which look similar to the example df below. I want to remove all rows before the value in the column le changes the first time, except for the last row with the duplicate value. E.g., I want to drop the first two rows in df here. However, I do not find a universal solutions for this I can use for all dataframes I have.

id     no     parent  le               dia
10     1           1  9.18359371679495  112.963635499912           
10     1           1  9.18359371679495  102.261060580237            
10     1           1  9.18359371679495  102.261060580237        
10     1           1   46.531309334225  75.1405324759379           
10     1           1   148.45737705256  68.9880315000758           
10     1           1  266.349709386555  68.9880315000758
10     1           1   352.40977395104  68.9880315000758        
10     1           1   352.40977395104  68.9880315000758       
10     1           1   352.40977395104  68.9880315000758 
...

I tried to approach this with deleting duplicates, but duplicates later then the first change should be kept. The final df should look like this:

id     no     parent  le                dia
10     1           1  9.18359371679495  102.261060580237        
10     1           1   46.531309334225  75.1405324759379           
10     1           1   148.45737705256  68.9880315000758           
10     1           1  266.349709386555  68.9880315000758
10     1           1   352.40977395104  68.9880315000758        
10     1           1   352.40977395104  68.9880315000758       
10     1           1   352.40977395104  68.9880315000758            
...
1 Answers

try:

df.drop_duplicates('le', keep='last')

id  no      parent  le          dia
10  1       1       9.183594    102.261061
10  1       1       46.531309   75.140532
10  1       1       148.457377  68.988032
10  1       1       266.349709  68.988032
Related