9951 explained code solutions for 126 technologies


python-pandasHow to replace strings in dataframe column


import pandas as pd

df = pd.DataFrame({
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Phone Price': [204, 304, 404, 405, 305]
})

df['Phone'] = df["Phone"].replace("ip", "Iphone", regex=True)ctrl + c
import pandas as pd

load Pandas module

pd.DataFrame

creates Pandas DataFrame object

df["Phone"]

column to find and replace values in

.replace(

replace values in given column

"ip"

substring to find

"Iphone"

value to replace by

regex=True

enables searching for substrings in string values


Usage example

import pandas as pd

df = pd.DataFrame({
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Phone Price': [204, 304, 404, 405, 305]
})

df['Phone'] = df["Phone"].replace("ip", "Iphone", regex=True)
print(df)
output
     Phone  Phone Price
0  Iphone5          204
1  Iphone6          304
2  Iphone8          404
3      sms          405
4       xi          305