OldSport
OldSport

Reputation: 137

Remove rows from dataframe based on index

I got a list containing row indexes to delete. The list contains duplicates so to lighten the burden I tried to remove duplicate entries:

invalid_rows = list(dict.fromkeys(invalid_rows))

Returning: TypeError: unhashable type: 'list'

Okey, well let's try with duplicate values just to see if I can remove the rows:

df = df.drop(index=invalid_rows)

Returning the same error code. What's the problem with the list? I tried to google but I can't seem to find the error.

Upvotes: 0

Views: 75

Answers (2)

itprorh66
itprorh66

Reputation: 3288

A simple way to remove duplicates from a list would be:

invalid_rows = list(set(invalid_rows))

This of course assumes invalid_rows is a list of hashable items

Upvotes: 1

Tugay
Tugay

Reputation: 2214

I am guessing that invalid_rows is not a list, it is actually a nested list. fromkeys method tries to use elements of that list as keys for dictionary, but since its elements (at least one of them) are not hashable type, it raises an error.

Upvotes: 1

Related Questions