Mapping external values to dataframe values in Pandas
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:
import pandas as pd
data = { 'First_name': ['Liam', 'Emma', 'Noah', 'Olivia', 'Ava'],
'Last_name': ['Smith', 'Brown', 'Davis', 'Wilson', 'Taylor'],
'Age': [42, 52, 36, 21, 23],
'City': ['New York', 'Paris', 'Berlin', 'Madrid', 'Rome'] }
df = pd.DataFrame(data)
print(df)
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.
1. Using map()
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.
new_vals = {"Liam": "MBA", "Emma": "PhD", "Noah": "LLB", "Olivia": "B.Tech", "Ava": "MD"}
df["Qualification"] = df["First_name"].map(new_vals)
print(df)
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.
2. Using replace()
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.
new_vals = {"Liam": "Lucas", "Noah": "Nathan", "Olivia": "Olive"}
df_replaced = df.replace({"First_name": new_vals})
print(df_replaced)
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.
3. Using update()
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.
new_vals = {0: "Lukas", 2: "Nicolas", 3: "Sophia"}
df["First_name"].update(pd.Series(new_vals))
print(df)
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.