Question on Pandas -> Exception Ignored: Unhashable Type: 'numpy.ndarray'

Viewed 615

I created a pandas DataFrame by grouping by two values and received a table similar to the one below:

First Name Column A
Samuel [1,2,3]
Joshua [4,5,6]
Richard [7,8,9]
Michael [7,8,9]
Henry [7,8,9]

Next, I grouped by Column A to figure out how many unique first names each list of numbers had, and received the error below:

Exception ignored in: 'pandas._libs.index.IndexEngine._call_map_locations'
Traceback (most recent call last):
  File "pandas\_libs\hashtable_class_helper.pxi", line 4588, in > pandas._libs.hashtable.PyObjectHashTable.map_locations
TypeError: unhashable type: 'numpy.ndarray'

I was confused when I was returned a pandas Series with the index being of type numpy array. Why was the exception ignored in this case?

1 Answers

try converting the list to tuple by df['Column A'].map(tuple) or df['Column A'].map(str)

df.groupby(df['Column A'].map(tuple))['First Name'].unique()

Column A
(1, 2, 3)                     [Samuel]
(4, 5, 6)                     [Joshua]
(7, 8, 9)    [Richard, Michael, Henry]
Name: First Name, dtype: object
Related