Friday, June 19, 2026
HomeLanguagesUsing dictionary to remap values in Pandas DataFrame columns

Using dictionary to remap values in Pandas DataFrame columns

While working with data in Pandas in Python, we perform a vast array of operations on the data to get the data in the desired form. One of these operations could be that we want to remap the values of a specific column in the DataFrame. Let’s discuss several ways in which we can do that.

Creating Pandas DataFrame to remap values

Given a Dataframe containing data about an event, remap the values of a specific column to a new value.

Python3




# importing pandas as pd
import pandas as pd
 
# Creating the DataFrame
df = pd.DataFrame({'Date': ['10/2/2011', '11/2/2011', '12/2/2011', '13/2/2011'],
                   'Event': ['Music', 'Poetry', 'Theatre', 'Comedy'],
                   'Cost': [10000, 5000, 15000, 2000]})
 
# Print the dataframe
print(df)


Output:

 

Remap values in Pandas columns using replace() function

Now we will remap the values of the ‘Event’ column by their respective codes using replace() function

Python3




# Create a dictionary using which we
# will remap the values
dict = {'Music' : 'M', 'Poetry' : 'P', 'Theatre' : 'T', 'Comedy' : 'C'}
 
# Print the dictionary
print(dict)
 
# Remap the values of the dataframe
df.replace({"Event": dict})


Output : 

 

 

Remap values in Pandas DataFrame columns using map() function 

Now we will remap the values of the ‘Event’ column by their respective codes using map() function

Python3




# Create a dictionary using which we
# will remap the values
dict = {'Music': 'M', 'Poetry': 'P', 'Theatre': 'T', 'Comedy': 'C'}
 
# Print the dictionary
print(dict)
 
# Remap the values of the dataframe
df['Event'] = df['Event'].map(dict)
 
# Print the DataFrame after modification
print(df)


Output: 

 

 

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6898 POSTS0 COMMENTS
Nicole Veronica
12014 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12110 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6965 POSTS0 COMMENTS