Using dictionary to remap values in Pandas DataFrame columns
Last Updated :
11 Jul, 2024
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.
Python
# 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:

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.Â
Python
# 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 :Â

Output

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.Â
Python
# 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:Â

Output

Output
Using dictionary to remap values in Pandas DataFrame columns – FAQs
How to Replace Values in a Pandas Series Using a Dictionary
To replace values in a pandas Series based on a dictionary where the keys represent the current values and the values represent the new values, you can use the replace() method:
import pandas as pd
# Example Series
series = pd.Series([1, 2, 3, 4, 1, 2, 3, 4])
# Dictionary mapping for replacement
replacement_dict = {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
# Replace values in the Series
series_replaced = series.replace(replacement_dict)
print(series_replaced)
How to Convert Dictionary to DataFrame with Keys as Columns
To convert a dictionary into a DataFrame where dictionary keys become column names and values become the data in the columns, you can simply pass the dictionary to the DataFrame constructor:
# Dictionary with lists as values
data_dict = {
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
}
# Convert to DataFrame
df = pd.DataFrame(data_dict)
print(df)
How to Rename a Column in Pandas Based on Dictionary
To rename columns in a DataFrame using a dictionary where the current column names are keys and the new names are values, use the rename() method with the columns parameter:
# DataFrame
df = pd.DataFrame({
'X': [1, 2],
'Y': [3, 4]
})
# Dictionary for renaming columns
rename_dict = {'X': 'New_X', 'Y': 'New_Y'}
# Rename columns
df_renamed = df.rename(columns=rename_dict)
print(df_renamed)
How to Remap Column Names in Pandas
Remapping column names can be done similarly by using the rename() method. It’s particularly useful when only some of the columns need to be renamed, and it aligns well with the renaming based on a dictionary:
# Continuing with the previous example
remap_dict = {'New_X': 'A', 'New_Y': 'B'}
df_remap = df_renamed.rename(columns=remap_dict)
print(df_remap)
How to Convert Column Values to Dictionary in Pandas
To convert the values of a DataFrame column to a dictionary where indices become keys, use the to_dict() method on the Series:
# Convert a single DataFrame column to a dictionary
column_dict = df['A'].to_dict()
print(column_dict)
# To convert the entire DataFrame to a dictionary of dictionaries
full_dict = df.to_dict()
print(full_dict)