It seemed to me that I correctly understood the card and applied the card, but I have a problem (see here for an additional background, if it is interesting).
A simple example:
df = pd.DataFrame( [[1,2],[1,1]] ) dct = { 1:'python', 2:'gator' } df[0].map( lambda x: x+90 ) df.applymap( lambda x: x+90 )
This works as expected - and works on an elementary basis, on a map on a map, overlays a map on a framework (explained very well here , btw).
If I use a dictionary, not lambda, the card still works fine:
df[0].map( dct ) 0 python 1 python
but map does not apply:
df.applymap( dct ) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-100-7872ff604851> in <module>() ----> 1 df.applymap( dct ) C:\Users\johne\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\frame.pyc in applymap(self, func) 3856 x = lib.map_infer(_values_from_object(x), f) 3857 return lib.map_infer(_values_from_object(x), func) -> 3858 return self.apply(infer) 3859 3860 #---------------------------------------------------------------------- C:\Users\johne\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\frame.pyc in apply(self, func, axis, broadcast, raw, reduce, args, **kwds) 3687 if reduce is None: 3688 reduce = True -> 3689 return self._apply_standard(f, axis, reduce=reduce) 3690 else: 3691 return self._apply_broadcast(f, axis) C:\Users\johne\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\frame.pyc in _apply_standard(self, func, axis, ignore_failures, reduce) 3777 try: 3778 for i, v in enumerate(series_gen): -> 3779 results[i] = func(v) 3780 keys.append(v.name) 3781 except Exception as e: C:\Users\johne\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\frame.pyc in infer(x) 3855 f = com.i8_boxer(x) 3856 x = lib.map_infer(_values_from_object(x), f) -> 3857 return lib.map_infer(_values_from_object(x), func) 3858 return self.apply(infer) 3859 C:\Users\johne\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\lib.pyd in pandas.lib.map_infer (pandas\lib.c:56990)() TypeError: ("'dict' object is not callable", u'occurred at index 0')
So my question is, why aren't we matching and applying work with a similar way here? Is this a mistake with applymap, or am I doing something wrong?
Edit to add . I found that I can pretty easily handle this:
df.applymap( lambda x: dct[x] ) 0 1 0 python gator 1 python python
Or even better, using an answer that does not require lambda.
df.applymap( dct.get )
So this is almost exactly equivalent, isn't it? There must be something with how applymap parses the syntax, and I assume the explicit form of the function / method works better than the dictionary. In any case, I think that now there is no practical problem, but here I am interested in what happens here if someone wants to answer.