2 minute read

Pandas has a cool feature called Map which let you create a new column by mapping the dataframe column values with the Dictionary Key. Let’s understand this by an example:

Create a Dataframe:

Let’s start by creating a dataframe of top 5 countries with their population

import pandas as pd
df= pd.DataFrame({'Country':['China','India','USA','Indonesia','Brazil'],'Population':[1403500365,1324171354,                                                                                     322179605,261115456,                                                                                     207652865]})

Create a Dictionary

This dictionary contains the countries and their corresponding National capitals, Where country is the Key and Capital is the value

'India':'New Delhi',
'Indonesia': 'Jakarta',

Now we have a dataframe of top 5 countries and their population and a dictionary which holds the country as Key and their National Capitals as value pair. Let’s create a new column called capital in the dataframe matching the Key value pair from the country column

Create Column Capital matching Dictionary value

df['Capital'] = df['Country'].map(country_capital)

Voila!! So we have created a new column called Capital which has the National capital of those five countries using the matching dictionary value

Map Accepts a Function Also

Let’s multiply the Population of this dataframe by 100 and store this value in a new column called as inc_Population

df['inc_Population']=df.Population.map(lambda x: x*100)

Pandas Replace from Dictionary Values

We will now see how we can replace the value of a column with the dictionary values

Create a Dataframe

Let’s create a dataframe of five Names and their Birth Month

df= pd.DataFrame({'Name':['Allan','John','Peter','Brenda','Sandra'],'birth_Month':[5,3,8,12,2]})

Create a Dictionary of Months

Let’s create a dictionary containing Month value as Key and it’s corresponding Name as Value



Let’s replace the birth_Month in the above dataframe with their corresponding Names

df['birth_Month'] = df.birth_Month.replace(country_capital)

Pandas Update column with Dictionary values matching dataframe Index as Keys

We will use update where we have to match the dataframe index with the dictionary Keys

Lets use the above dataframe and update the birth_Month column with the dictionary values where key is meant to be dataframe index, So for the second index 1 it will be updated as January and for the third index i.e. 2 it will be updated as February and so on


There is no matching value for index 0 in the dictionary that’s why the birth_Month is not updated for that row and all other rows the value is updated from the dictionary matching the dataframe indexes

Tags: , ,

Categories: , ,