Map vs applymap when passing the dictionary

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.

+7
python pandas
source share
1 answer

.applymap () and .map () are correct for working on elements. But .applymap () does not accept all columns and does .map () on them, but does .apply () for each of them.

So when you call df.applymap (dct): What happens df [0] .apply (dct), and not df [0] .map (dct)

And what is the difference between these two methods of the Series:

.map () take Series, dict, and a function (any called, so methods like dict.get, too) as the first argument; since .apply () only allows a function (or any called) as the first argument.

.map () contains an if statement to find out if the first argument, dict, series, or function passed and act on the input. When you pass a function to .map (), the .map () method does the same as .apply ().

But .apply () do not have those if statements that allow it to use proprely with dictionnary and Series. He only knows how to work with the callee.

When you call .apply () or .map () with a function, they both end the lib.map_infer () call, which look like acting like a python map () function (but Im letting me put my hand in the source code, so I don’t completely sure).

Running the map (dct, df [0]) will give you the same error as df.applymap (dct) and df [0] .apply (dct) will also give the same error.

Now you may ask why using .apply () instead of .map () if .map () does the same when called with a function and can accept dict and Series?

Because .apply () can return a Dataframe to you if the result of the function you pass to it is a series.

 ser = pandas.Series([1,2,3,4,5], index=range(5)) ser_map = ser.map(lambda x : pandas.Series([x]*5, index=range(5))) type(ser_map) pandas.core.series.Series ser_app = ser.apply(lambda x : pandas.Series([x]*5, index=range(5))) type(ser_app) pandas.core.frame.DataFrame 
+5
source share

All Articles