While working with data in Pandas, 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.
Given a Dataframe containing data about an event, remap the values of a specific column to a new value.
Code #1: We can use DataFrame.replace() function to achieve this task. Let’s see how we can do that.
# 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 :

Now we will remap the values of the ‘Event’ column by their respective codes.
# 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 :


Code #2: We can use map() function to achieve this task.
# 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 :

Now we will remap the values of the ‘Event’ column by their respective codes.
# 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 :


Recommended Posts:
- Sort rows or columns in Pandas Dataframe based on values
- Drop rows from Pandas dataframe with missing values or NaN in columns
- Find maximum values & position in columns and rows of a Dataframe in Pandas
- Get minimum values in rows or columns with their index position in Pandas-Dataframe
- Python | Pandas DataFrame.fillna() to replace Null values in dataframe
- How to rename columns in Pandas DataFrame
- Add multiple columns to dataframe in Pandas
- Difference of two columns in Pandas dataframe
- Concatenate two columns of Pandas dataframe
- Python | Pandas DataFrame.columns
- Conditional operation on Pandas DataFrame columns
- How to drop one or multiple columns in Pandas Dataframe
- How to select multiple columns in a pandas dataframe
- Iterating over rows and columns in Pandas DataFrame
- Getting frequency counts of a columns in Pandas DataFrame
- Count number of columns of a Pandas DataFrame
- Dealing with Rows and Columns in Pandas DataFrame
- Loop or Iterate over all or certain columns of a dataframe in Python-Pandas
- Count the number of rows and columns of Pandas dataframe
- Change the order of a Pandas DataFrame columns in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

