pandas TypeError: unhashable type: 'list' (drop_duplicates, groupby)
TypeError: unhashable type: 'list' in pandas means a column holds Python lists and you called something that hashes cells: drop_duplicates, groupby or nunique. Convert the column with .apply(tuple) or explode it. Tested on pandas 2.3.3 and 3.1.0.
1. Which pandas methods fail on list cells
import pandas as pd
df = pd.DataFrame({"user": ["a", "b", "a"], "tags": [["x", "y"], ["z"], ["x", "y"]]})
for name, call in [("drop_duplicates", lambda: df.drop_duplicates()),
("value_counts", lambda: df["tags"].value_counts()),
("groupby", lambda: df.groupby("tags").size()),
("nunique", lambda: df["tags"].nunique())]:
try:
call()
print(name, "OK")
except TypeError as e:
print(name, "-> TypeError:", e)
Output, identical on pandas 2.3.3 and 3.1.0:
drop_duplicates -> TypeError: unhashable type: 'list'
value_counts OK
groupby -> TypeError: unhashable type: 'list'
nunique -> TypeError: unhashable type: 'list'
drop_duplicates, groupby and nunique hash each cell, and a Python list cannot be hashed. value_counts did not raise on either version, so code that works in one place can fail one line later. List cells usually come from JSON (read_json, json_normalize) or from str.split().
2. Fix: convert to tuples, or explode
import pandas as pd
df = pd.DataFrame({"user": ["a", "b", "a"], "tags": [["x", "y"], ["z"], ["x", "y"]]})
df["tags_t"] = df["tags"].apply(tuple) # lists -> hashable tuples
print(df.drop_duplicates(subset=["user", "tags_t"])[["user", "tags"]])
print(df["tags_t"].value_counts().to_dict())
print(df.explode("tags")["tags"].value_counts().to_dict()) # count single tags
Output, identical on pandas 2.3.3 and 3.1.0:
user tags
0 a [x, y]
1 b [z]
{('x', 'y'): 2, ('z',): 1}
{'x': 2, 'y': 2, 'z': 1}
A tuple column with the same contents is hashable, so deduplicating and counting on it works while the original list column stays for display. Use explode when you want one row per element (counting individual tags), not per list. If the lists are only an accident of str.split(), keep the string column for grouping instead. For the plain-Python version of this error, see TypeError: unhashable type: 'list'.
Related: unhashable type: 'Series' · groupby FutureWarnings · apply errors='coerce'.
Frequently asked questions
How do I drop duplicates when a column contains lists?
Make a tuple copy with df['col'].apply(tuple) and pass it in subset=; tuples are hashable.
Why does value_counts work but drop_duplicates fails on lists?
In our test value_counts did not raise on list cells while drop_duplicates, groupby and nunique did, on both pandas 2.3.3 and 3.1.0.
How do I count individual items inside list cells?
df.explode('col')['col'].value_counts() gives one row per element, then counts them.
Written with AI assistance. Measured on 2026-10-23 in the python:3-alpine Docker image (Python 3.14.7) with pandas 2.3.3 and pandas 3.1.0. Outputs pasted unedited. PlainTables is our extension; Python and pandas are free.