str.replace()

noun

say it: “S-T-R dot ree-PLAYSS” /ˌɛs ti ˈɑr dɑt rɪˈpleɪs/

A pandas tool that swaps one piece of text for another inside every value of a text column, such as removing a unit from each weight.

What does str.replace() do in pandas?

str.replace() swaps one piece of text for another inside every value of a pandas text column, such as taking " lbs" off "59 lbs". It changes part of each value; Series.replace() swaps whole values.

import pandas as pd
weights = pd.Series(["59 lbs", "12 lbs", "8 lbs"])
print(weights.str.replace(" lbs", ""))   # "59", "12", "8"

In a sentence

  • “str.replace() took " lbs" off every weight in one line.”
  • “She used str.replace() to remove the dollar signs before turning the prices into numbers.”

Level: Journeyman · More Python terms at this level

Updated