Search code examples
pandasdrop-duplicates

Pandas drop_duplicates. Keep first AND last. Is it possible?


I have this dataframe and I need to drop all duplicates but I need to keep first AND last values

For example:

1      0

2     0

3     0

4     0

output:

1     0

4     0

I tried df.column.drop_duplicates(keep=("first","last")) but it doesn't word, it returns

ValueError: keep must be either "first", "last" or False

Does anyone know any turn around for this?

Thanks


Solution

  • You could use the panda's concat function to create a dataframe with both the first and last values.

    pd.concat([
        df['X'].drop_duplicates(keep='first'),
        df['X'].drop_duplicates(keep='last'),
    ])