• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Pandas DataFrame / How To Add a New Column Using a Dictionary in Pandas Data Frame ?

How To Add a New Column Using a Dictionary in Pandas Data Frame ?

January 28, 2018 by cmdlinetips

add a new column with map in pandas
add a new column with map in pandas

Creating a new column to a dataframe is a common task in doing data analysis. And this task often comes in a variety of forms. Earlier we saw how to add a column using an existing columns in two ways. In this post we will learn how to add a new column using a dictionary in Pandas.

Pandas library in Python has a really cool function called map that lets you manipulate your pandas data frame much easily. Pandas’ map function lets you add a new column with values from a dictionary if the data frame has a column matching the keys in the dictionary.

Adding a New Column Using keys from Dictionary matching a column in pandas

Let us say you have pandas data frame created from two lists as columns; continent and mean_lifeExp.

# derived from gapminder data set
# http://bit.ly/2cLzoxH
print(gapminder_df)
  continent  mean_lifeExp
0      Asia        48.86
1    Europe        64.65
2    Africa        60.06
3  Americas        71.90
4   Oceania        74.32

and let us say we also have a dictionary, where the keys are “continent” as in the above data frame and values are mean population over years.

print(pop_dict)
{'Europe': 24504794.99, 
'Oceania': 8874672.33, 
'Africa': 77038721.97, 
'Asia': 9916003.14, 
'Americas': 17169764.73}

Note that it is a dictionary, so the order of items do not match the continent column of the data frame.

Map function to Add a New Column to pandas with Dictionary

Let us say we want to add a new column ‘pop’ in the pandas data frame with values from the dictionary. Note the keys of the dictionary are “continents” and the column “continent” in the data frame. Pandas’ map function is here to add a new column in pandas dataframe using the keys:values from the dictionary.

gapminder_df['pop']= gapminder_df['continent'].map(pop_dict)

Voila!! here is the updated data frame with a new column from the dictionary.

continent  mean_lifExp          pop
0      Asia        48.86   9916003.14
1    Europe        64.65  24504794.99
2    Africa        60.06  77038721.97
3  Americas        71.90  17169764.73
4   Oceania        74.32   8874672.33

Another common use of dictionary to add a new column in Pandas is to code an exisiting column using dictionary and create a new column. For example, let us consider the gapminder data frame

data_url = 'http://bit.ly/2cLzoxH'
# read data from url as pandas dataframe
gapminder = pd.read_csv(data_url)
gapminder = gapminder[['continent','gdpPercap', 'lifeExp']]
print(gapminder.head(3))

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow to Get Frequency Counts of a Column in Pandas Dataframe: Pandas Tutorial Pandas Filter/Select Rows Based on Column ValuesHow To Filter Pandas Dataframe By Values of Column? Default ThumbnailHow to Get Unique Values from a Column in Pandas Data Frame? Default ThumbnailHow To Read a CSV File as Pandas Data Frame?

Filed Under: Pandas DataFrame, Pandas map function, Python Tips Tagged With: Pandas Data Frame, Pandas map function, Python Tips

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version