![]() |
VOOZH | about |
Mapping external values in Pandas means using a dictionary to update or add data in a DataFrame. It’s a quick way to modify existing columns or create new ones with mapped values.
We are going to use the following DataFrame for examples in this article:
Output
First_name Last_name Age City
0 Liam Smith 42 New York
1 Emma Brown 52 Paris
2 Noah Davis 36 Berlin
3 Olivia Wilson 21 Madrid
4 Ava Taylor 23 Rome
Now, let’s explore the different methods one by one.
map() function applies external values to a column based on dictionary keys. It is best suited for adding a new column when each key corresponds uniquely to a value.
Example: In this example, we add a new column Qualification using external values mapped to the first names.
Output
First_name Last_name Age City Qualification
0 Liam Smith 42 New York MBA
1 Emma Brown 52 Paris PhD
2 Noah Davis 36 Berlin LLB
3 Olivia Wilson 21 Madrid B.Tech
4 Ava Taylor 23 Rome MD
Explanation: The dictionary keys are matched with First_name and corresponding values are added as a new column.
replace() function substitutes column values based on a dictionary. It is useful when we want to replace or update multiple values directly.
Example: In this example, we will replace some first names with new ones.
Output
First_name Last_name Age City Qualification
0 Lucas Smith 42 New York MBA
1 Emma Brown 52 Paris PhD
2 Nathan Davis 36 Berlin LLB
3 Olive Wilson 21 Madrid B.Tech
4 Ava Taylor 23 Rome MD
Explanation: The values in the First_name column are replaced with those specified in the dictionary.
update() method modifies values in place using index-based mapping. It works best for targeted updates but is less flexible than the other methods.
Example: In this example, we will update specific first names based on row indices.
Output
First_name Last_name Age City Qualification
0 Lukas Smith 42 New York MBA
1 Emma Brown 52 Paris PhD
2 Nicolas Davis 36 Berlin LLB
3 Sophia Wilson 21 Madrid B.Tech
4 Ava Taylor 23 Rome MD
Explanation: The dictionary keys correspond to row indices and the values replace the First_name column entries at those positions.