You can use .replace. For example:

>>> df = pd.DataFrame({'col2': {0: 'a', 1: 2, 2: np.nan}, 'col1': {0: 'w', 1: 1, 2: 2}})
>>> di = {1: "A", 2: "B"}
>>> df
  col1 col2
0    w    a
1    1    2
2    2  NaN
>>> df.replace({"col1": di})
  col1 col2
0    w    a
1    A    2
2    B  NaN

or directly on the Series, i.e. df["col1"].replace(di, inplace=True).

Answer from DSM on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › using-dictionary-to-remap-values-in-pandas-dataframe-columns
Using dictionary to remap values in Pandas DataFrame columns - GeeksforGeeks
July 11, 2025 - The replace() function in Pandas allows us to remap values using a dictionary. It works directly on a DataFrame column and modifies the values based on the provided mapping.
Top answer
1 of 12
628

You can use .replace. For example:

>>> df = pd.DataFrame({'col2': {0: 'a', 1: 2, 2: np.nan}, 'col1': {0: 'w', 1: 1, 2: 2}})
>>> di = {1: "A", 2: "B"}
>>> df
  col1 col2
0    w    a
1    1    2
2    2  NaN
>>> df.replace({"col1": di})
  col1 col2
0    w    a
1    A    2
2    B  NaN

or directly on the Series, i.e. df["col1"].replace(di, inplace=True).

2 of 12
603

map can be much faster than replace

If your dictionary has more than a couple of keys, using map can be much faster than replace. There are two versions of this approach, depending on whether your dictionary exhaustively maps all possible values (and also whether you want non-matches to keep their values or be converted to NaNs):

Exhaustive Mapping

In this case, the form is very simple:

df['col1'].map(di)       # note: if the dictionary does not exhaustively map all
                         # entries then non-matched entries are changed to NaNs

Although map most commonly takes a function as its argument, it can alternatively take a dictionary or series: Documentation for Pandas.series.map

Non-Exhaustive Mapping

If you have a non-exhaustive mapping and wish to retain the existing variables for non-matches, you can add fillna:

df['col1'].map(di).fillna(df['col1'])

as in @jpp's answer here: Replace values in a pandas series via dictionary efficiently

Benchmarks

Using the following data with pandas version 0.23.1:

di = {1: "A", 2: "B", 3: "C", 4: "D", 5: "E", 6: "F", 7: "G", 8: "H" }
df = pd.DataFrame({ 'col1': np.random.choice( range(1,9), 100000 ) })

and testing with %timeit, it appears that map is approximately 10x faster than replace.

Note that your speedup with map will vary with your data. The largest speedup appears to be with large dictionaries and exhaustive replaces. See @jpp answer (linked above) for more extensive benchmarks and discussion.

Discussions

How do I map the keys of a dictionary to the columns of a pandas dataframe?

Keep this paradigm in your head: dataframes are meant to be instantiated from a data source, they aren't meant to be created as a blank sheet and 'filled in' later on. So also in this case: don't build a dataframe upfront, instead create the dataframe using the dict as the basis for its data. From Create a Pandas DataFrame from List of Dicts:

cols = ['timestamp', 'name', 'adress', 'phone', 'website', 'rating', 'number_of_ratings', 'type']
df = pd.DataFrame(multidict, columns=cols)
More on reddit.com
🌐 r/learnpython
5
7
January 12, 2020
python - How to map column of lists with values in a dictionary using pandas - Stack Overflow
I'm new to pandas and I want to know if there is a way to map a column of lists in a dataframe to values stored in a dictionary. Lets say I have the dataframe 'df' and the dictionary 'dict'. I want... More on stackoverflow.com
🌐 stackoverflow.com
How to remap values in Pandas column using a dictionary, while also preserving any NaN values that may be present? - Python - Data Science Dojo Discussions
I am trying to remap values in a specific column of a Pandas DataFrame using a dictionary, while ensuring that any NaN values in the column are preserved and not modified. I am looking for alternative ways to achieve this using Pandas, and would appreciate any suggestions from the community. More on discuss.datasciencedojo.com
🌐 discuss.datasciencedojo.com
1
0
April 28, 2023
python - How do you map a dictionary to an existing pandas dataframe column? - Stack Overflow
I would like to take the dictionary and use that to fill in missing values in a dataframe column. So the dictionary keys correspond to the index in the dataframe or a different column in the data f... More on stackoverflow.com
🌐 stackoverflow.com
🌐
DataScientYst
datascientyst.com › pandas-map-column-dictionary
How to Map Column with Dictionary in Pandas
March 29, 2025 - The result is a new Pandas Series with the mapped values: 0 False 1 True 2 False 3 True Name: Disqualified, dtype: object · We can assign this result Series to the same column by: df['Disqualified'] = df['Disqualified'].map(dict_map) To map dictionary from existing column to new column we need to change column name:
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas remap values in column with a dictionary (dict)
Pandas Remap Values in Column with a Dictionary (Dict) - Spark By {Examples}
December 10, 2024 - We are often required to remap a Pandas DataFrame column values with a dictionary (Dict), you can achieve this by using the DataFrame.replace() method.
🌐
Reddit
reddit.com › r/learnpython › how do i map the keys of a dictionary to the columns of a pandas dataframe?
r/learnpython on Reddit: How do I map the keys of a dictionary to the columns of a pandas dataframe?
January 12, 2020 -

Hi y'all,

i have a list of dictionaries (multidict), which holds a lot of different values. I also have an empty dataframe with set columns which do not coincide with keys of the dictionaries.

I need to append each dictionary to the DataFrame and map each key of the dictionary to the right (and fixed) column. How do i pass the values for each dictionary to the right column and row?

Here is my code so far with exemplary data:

multidict = [{'website': 'http://www.example.de/', 'time': '2020-01-05 17:33:53.973205', 'norating': 112, 'name': 'Name', 'rating': 4.4, 'phone': '123456789', 'adress': 'adress', 'typus': "['restaurant', 'bowling_alley', 'lodging', 'food', 'point_of_interest', 'establishment']"},{'website': 'http://www.exmpl.de/', 'time': '2020-02-05 17:33:53.973205', 'norating': 12, 'name': 'Name2', 'rating': 4.3, 'phone': '987654321', 'adress': 'address', 'typus': "['whatever', 'point_of_interest', 'establishment']"}]

df = pd.DataFrame()
df.columns = ['timestamp', 'name', 'adress', 'phone', 'website', 'rating', 'number_of_ratings', 'type']

for i in multidict:
    #How do I remap the keys of the dictionary here to the columns of the dataframe???
🌐
Kanoki
kanoki.org › 2019 › 04 › 06 › pandas-map-dictionary-values-with-dataframe-columns
Pandas Map Dictionary values with Dataframe Columns | kanoki
April 6, 2019 - So we have created a new column called Capital which has the National capital of those five countries using the matching dictionary value ... 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)
🌐
Rip Tutorial
riptutorial.com › map from dictionary
pandas Tutorial => Map from Dictionary
Imagine you want to add a new column called S taking values from the following dictionary: d = {112: 'en', 113: 'es', 114: 'es', 111: 'en'} You can use map to perform a lookup on keys returning the corresponding values as a new column: df['S'] = df['U'].map(d) that returns: U L S 111 en en 112 en en 112 es en 113 es es 113 ja es 113 zh es 114 es es · PDF - Download pandas for free · Previous Next · SUPPORT & PARTNERS · Advertise with us ·
Find elsewhere
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
How to remap values in Pandas column using a dictionary, while also preserving any NaN values that may be present? - Python - Data Science Dojo Discussions
April 28, 2023 - I am trying to remap values in a specific column of a Pandas DataFrame using a dictionary, while ensuring that any NaN values in the column are preserved and not modified. I am looking for alternative ways to achieve thi…
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.map.html
pandas.Series.map — pandas 3.0.6 documentation
Map values of Series according to an input mapping or function. Used for substituting each value in a Series with another value, that may be derived from a function, a dict or a Series.
🌐
datagy
datagy.io › home › pandas tutorials › pandas dataframes › transforming pandas columns with map and apply
Transforming Pandas Columns with map and apply • datagy
March 20, 2023 - Pandas provides a wide array of ... them to multiple records at the same time · The Pandas .map() method can pass in a dictionary to map values to a dictionaries keys...
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Replace Series values with map() | note.nkmk.me
January 17, 2024 - Note that functionality may vary between versions. ... When a dictionary (dict) is specified in map(), values in the Series matching a dictionary key are replaced with the corresponding dictionary value.
🌐
ProjectPro
projectpro.io › recipes › map-values-in-pandas-dataframe
How to map values in a Pandas DataFrame? -
September 1, 2023 - Get Closer To Your Dream of Becoming a Data Scientist with 70+ Solved End-to-End ML Projects · The following steps will help you understand how to map Pandas dataframe, i.e., map column values in Pandas Dataframe. ... We have imported the Pandas library, which is needed to perform Pandas Dataframe map values. We have created a dataset by making a dictionary with features and passing it through the dataframe function.
Top answer
1 of 2
3

You can use join by Series with MultiIndex:

idx = pd.MultiIndex.from_product([[0,1],[0,1]], names=('q1','q2'))
s = pd.Series(['a','b','c','d'], index=idx, name='val')
print (s)
q1  q2
0   0     a
    1     b
1   0     c
    1     d
Name: val, dtype: object

df = df.join(s, on=['q1','q2'])
print (df)
    q1  q2 val
0    0   1   b
1    0   1   b
2    0   1   b
3    0   1   b
4    0   1   b
5    0   1   b
6    0   1   b
7    0   1   b
8    0   1   b
9    0   1   b
10   1   1   d
11   1   1   d
12   0   1   b
13   0   1   b
14   1   0   c
15   0   0   a
16   0   0   a
17   0   0   a
18   0   0   a
19   0   0   a
20   0   0   a
21   0   0   a
2 of 2
1

Another method with df.map and df.transform:

In [90]: mapping = {(0, 0) :'a', (0, 1) : 'b', (1, 0): 'c', (1, 1): 'd'}

In [91]: df['val'] = df.transform(lambda x: (x['q1'], x['q2']), axis=1).map(mapping); df
Out[91]: 
    q1  q2 val
0    0   1   b
1    0   1   b
2    0   1   b
3    0   1   b
4    0   1   b
5    0   1   b
6    0   1   b
7    0   1   b
8    0   1   b
9    0   1   b
10   1   1   d
11   1   1   d
12   0   1   b
13   0   1   b
14   1   0   c
15   0   0   a
16   0   0   a
17   0   0   a
18   0   0   a
19   0   0   a
20   0   0   a
21   0   0   a

You can also use zip to generate columns, apply pd.Series and then do the mapping:

In [119]: df['val'] = pd.Series(list(zip(df.q1, df.q2))).map(mapping); df
Out[119]: 
    q1  q2 val
0    0   1   b
1    0   1   b
2    0   1   b
3    0   1   b
4    0   1   b
5    0   1   b
6    0   1   b
7    0   1   b
8    0   1   b
9    0   1   b
10   1   1   d
11   1   1   d
12   0   1   b
13   0   1   b
14   1   0   c
15   0   0   a
16   0   0   a
17   0   0   a
18   0   0   a
19   0   0   a
20   0   0   a
21   0   0   a

Performance

jezrael's solution:

In [552]: %%timeit
     ...: idx = pd.MultiIndex.from_product([[0,1],[0,1]], names=('q1','q2'))
     ...: s = pd.Series(['a','b','c','d'], index=idx, name='val')
     ...: df.join(s, on=['q1','q2'])
     ...: 
100 loops, best of 3: 2.84 ms per loop

Proposed in this post:

In [553]: %%timeit
     ...: mapping = {(0, 0) :'a', (0, 1) : 'b', (1, 0): 'c', (1, 1): 'd'}
     ...: df.transform(lambda x: (x['q1'], x['q2']), axis=1).map(mapping)
     ...:  
1000 loops, best of 3: 1.7 ms per loop
🌐
YouTube
youtube.com › watch
Python Pandas Map function | Zip | Use of python dictionary for mapping the values of a column - YouTube
Python Pandas Map function | Zip | Use of python dictionary for mapping the values of a columnPython for Machine Learning - Session # 92Topic to be covered -...
Published: December 13, 2018