Search code examples
pythondataframeconditional-statementsrow

How to delete just some rows in a dataframe according with some fields condition in Python?


I need to keep just the first row of a dataframe for each group of values in a ordered column.

I need to transform this (first column is ordered by name):

a = [[1,'a'],[1,'c'],[1,'b'],[2,'c'],[2,'b'],[2,'a'],[3,'d']]

into this (just the first row for each value type in the first ordered column):

a = [[1,'a'],[2,'c'],[3,'d']]

Solution

  • You can even get your desire output by convert to DF and by keeping the first value of each unique values of first column. lastly, converting back to list.

    Code:

    import pandas as pd
    
    a = [[1,'a'],[1,'c'],[1,'b'],[2,'c'],[2,'b'],[2,'a'],[3,'d']]
    a = pd.DataFrame(a).drop_duplicates(0, keep='first').values.tolist()