Replace and Rename
In this lesson, functions to replace values and renaming indexes are discussed.
We'll cover the following...
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.
Press + to interact
import numpy as npimport pandas as pdsrs = 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 ...