Lin Ma Lin Ma - 3 months ago 28
Python Question

pandas group by confusion -- unhashable type

Using Pandas data frame group by feature and I want to group by column

c_b
and calculate unique count for column
c_a
and column
c_c
. My expected results are,

Expected results,

c_b,c_a_unique_count,c_c_unique_count
python,2,2
c++,2,2


Met with strange error about
unhashable type
, does anyone have any ideas? Thanks.

Input file,

c_a,c_b,c_c,c_d
hello,python,numpy,0.0
hi,python,pandas,1.0
ho,c++,vector,0.0
ho,c++,std,1.0
go,c++,std,0.0


Source code,

sample = pd.read_csv('123.csv', header=None, skiprows=1,
dtype={0:str, 1:str, 2:str, 3:float})
sample.columns = pd.Index(data=['c_a', 'c_b', 'c_c', 'c_d'])
sample['c_d'] = sample['c_d'].astype('int64')
sampleGroup = sample.groupby('c_b')
results = sampleGroup.count()[:,[0,2]]
results.to_csv(derivedFeatureFile, index= False)


Error message,

Traceback (most recent call last):
File "/Users/foo/personal/featureExtraction/kaggleExercise.py", line 134, in <module>
unitTest()
File "/Users/foo/personal/featureExtraction/kaggleExercise.py", line 129, in unitTest
results = sampleGroup.count()[:,[0,2]]
File "/Users/foo/miniconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 1997, in __getitem__
return self._getitem_column(key)
File "/Users/foo/miniconda2/lib/python2.7/site-packages/pandas/core/frame.py", line 2004, in _getitem_column
return self._get_item_cache(key)
File "/Users/foo/miniconda2/lib/python2.7/site-packages/pandas/core/generic.py", line 1348, in _get_item_cache
res = cache.get(item)
TypeError: unhashable type

Answer

For the number of unique elements in each group, you can use:

df.groupby('c_b')['c_a', 'c_d'].agg(pd.Series.nunique)

df.groupby('c_b')['c_a', 'c_d'].agg(pd.Series.nunique)
Out: 
        c_a  c_d
c_b             
c++       2    2
python    2    2

df.groupby('c_b', as_index=False)['c_a', 'c_d'].agg(pd.Series.nunique)
Out: 
      c_b  c_a  c_d
0     c++    2    2
1  python    2    2
Comments