9951 explained code solutions for 126 technologies


python-pandasHow to replace values in dataframe


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("ip6", "Iphone 6")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

"ip6"

value to find

"Iphone 6"

value to replace by


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("ip6", "Iphone 6")
print(df)
output
      Phone  Phone Price
0       ip5          204
1  Iphone 6          304
2       ip8          404
3       sms          405
4        xi          305