Reputation: 2498
When I run a drop_duplicates
on my DataFrame I am getting an unhashable type: list
error. It's because my df
contains lists. However, it's non-obvious to me that it does. When I run df.dtypes
the columns return as object
.
I could do df.apply(lambda x: str(x) if isinstance(x, list) else x)
, which stringifies everything. However there are other unhashable types I would have to try. Like, for all I know there are dictionaries or tuples holding mutables, and these would give the same error
In order to overcome this, I have had to manually inspect my df to figure which columns are list, and handle them accordingly (dropping them worked).I don't like this. Is there an easier pandas command to return columns containing lists? For example, might there be something like, if my dataframe is:
df = pd.DataFrame([[1, 'a', [1,2,3]], [2, "b", [4,5,6]]], columns = ["number", "letter", "unhashable"])
Which looks like:
number letter unhashable
0 1 a [1, 2, 3]
1 2 b [4, 5, 6]
Then there's a method I can run on df
, something like df.detailed_dtypes
, to give me back
number: int,
letter: str,
unhashable: list
Upvotes: 1
Views: 424
Reputation: 323276
Try with
df.iloc[0].map(lambda x : type(x).__name__)
Out[136]:
number int64
letter str
unhashable list
Name: 0, dtype: object
Upvotes: 3