‘map’ in python

In some of my previous posts (Google while coding…, Functional tools in python.., Applying operations over dataframes)  I have noted the use of the ‘map keyword in pandas.

‘map is used in Python for the following scenarios :

  • as a functional operator in Python
    • Return a list of the results of applying the function to the items of
      the argument sequence(s)
  • as an element-wise function on a Series
  • pyspark



#Problem : that you've got two collections of values and you need to keep the largest (or smallest) from each. These could be metrics from two different systems, stock quotes from two different services, or just about anything. 

a = [1, 2, 3, 4, 5]
b = [2, 2, 9, 0, 9]

#Approach 1.
maxval = []
for i in range(len(a)):
    if a[i] >= b[i]:

#Approach 2.
print map(lambda pair: max(pair), zip(a,b))

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 3), columns=list('bde'), index=['Seattle', 'Utah', 'Ohio', 'Texas', 'Oregon'])

#1. Using DataFrame 'apply'
#applying a function on 1D arrays to each column or row.
f = lambda x: x.max() - x.min()

#2. Using DataFrame 'applymap'
#Element-wise Python functions can be used, too. Suppose you wanted to compute a formatted string from each floating point value in frame. You can do this with applymap
formatf = lambda x: '%.2f' % x

#3. Using Series 'map'
formatf = lambda x: '%.2f' % x
df['f'] = df['e'].map(formatf)

#Summing up, apply works on a row / column basis of a DataFrame, applymap works element-wise on a DataFrame, and map works element-wise on a Series



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s