Search⌘ K
AI Features

Replace and Rename

Explore how to effectively replace values in pandas Series and DataFrames using the replace() function and rename row and column indexes with the rename() function. This lesson helps you clean and manage data for predictive analysis by applying these essential data wrangling techniques.

Replace values

The pandas package provides built-in functions to replace values in a Series and a DataFrame. The replace() function is called by both Series and DataFrame objects to execute this task.

Replace in Series #

The following example shows how to replace single and multiple values in a Series object.

Python 3.5
import numpy as np
import pandas as pd
srs = pd.Series(['A','B','C','D','D','C','A','B'])
print("Original series:")
print(srs, '\n')
print("Single value changed in Series:")
print(srs.replace('A', np.nan), '\n')
print("Multiple values changed in Series:")
print(srs.replace(['A','C'], [1, 3]))

On line 10, a single value A is replaced with ...