Pandas Dataframe: How to drop_duplicates() based on index subset?

Viewed 54

Wondering if someone could please help me on this:

Have a pandas df with a rather large amount of columns (over 50). I'd like to remove duplicates based on a subset (column 2 to 50).

Been trying to use df.drop_duplicates(subset=["col1","col2",....]), but wondering if there is a way to pass the column index instead so I don't have to actually write out all the column headers to consider for the drop but instead can do something along the lines of df.drop_duplicates(subset = [2:])

Thanks upfront

1 Answers

You can slice df.columns like:

df.drop_duplicates(subset = df.columns[2:])

Or:

df.drop_duplicates(subset = df.columns[2:].tolist())
Related