import pandas as pd
# Create a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': ['a', 'b', 'c', 'd', 'e']})
# Replace all occurrences of 2 with 0
df['A'] = df['A'].replace(2, 0)
print(df)
import pandas as pd
# Create a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': ['a', 'b', 'c', 'd', 'e']})
# Replace values using a mapping provided in a dictionary
df['B'] = df['B'].replace({'a': 'A', 'c': 'C'})
print(df)
import pandas as pd
# Create a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': ['a', 'b', 'c', 'd', 'e']})
# Replace all values that start with the letter 'c' with 'X'
df['B'] = df['B'].replace('^c', 'X', regex=True)
print(df)