I thought I understood map vs applymap pretty well, but am having a problem (see here for additional background, if interested).
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 )
That works as expected -- both operate on an elementwise basis, map on a series, applymap on a dataframe (explained very well here btw).
If I use a dictionary rather than a lambda, map still works fine:
df[0].map( dct )
0 python
1 python
but not applymap:
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 don't map and applymap work in an analogous manner here? Is it a bug with applymap, or am I doing something wrong?
Edit to add: I have discovered that I can work around this fairly easily with this:
df.applymap( lambda x: dct[x] )
0 1
0 python gator
1 python python
Or better yet via this answer which requires no lambda.
df.applymap( dct.get )
So that is pretty much exactly equivalent, right? Must be something with how applymap parses the syntax and I guess the explicit form of a function/method works better than a dictionary. Anyway, I guess now there is no practical problem remaining here but am still interested in what is going on here if anyone wants to answer.