Pandas map vs replace

Pandas map vs replace

Pandas map:

The map function in Pandas is used to substitute each value in a Series or DataFrame column with another value.
It takes a dictionary, Series, or a function as an input to specify the substitution. The map function is mainly used when we want to perform element-wise transformations on a column in a DataFrame, based on a mapping defined by the user.

Example:

import pandas as pd

# Creating a simple DataFrame
data = {'Name': ['John', 'Emily', 'James', 'Mary'],
        'Age': [25, 30, 35, 40]}
df = pd.DataFrame(data)

# Creating a mapping dictionary
mapping = {'John' : 'Male', 'Emily' : 'Female', 'James' : 'Male', 'Mary' : 'Female'}

# Using map to substitute values in 'Name' column
df['Gender'] = df['Name'].map(mapping)

print(df)
  

This will output:

    Name  Age  Gender
0   John   25    Male
1  Emily   30  Female
2  James   35    Male
3   Mary   40  Female
  

Pandas replace:

The replace function in Pandas is used to substitute a specified value in a Series or DataFrame with another value.
Unlike map, the replace function can operate on the entire DataFrame at once, and it doesn’t require a mapping dictionary.
We can simply specify the value to be replaced and the value to replace it with.

Example:

import pandas as pd

# Creating a simple DataFrame
data = {'Name': ['John', 'Emily', 'James', 'Mary'],
        'Age': [25, 30, 35, 40]}
df = pd.DataFrame(data)

# Replacing values in the 'Name' column
df['Name'].replace('John', 'Jonathan', inplace=True)

print(df)
  

This will output:

     Name  Age
0    Jonathan   25
1  Emily   30
2  James   35
3   Mary   40
  

In this example, we replaced the value ‘John’ with ‘Jonathan’ in the ‘Name’ column of the DataFrame.

Leave a comment