df=df.replace('Max','Jim') # replace in all columns
Output
id name class1 mark gender
0 1 John Four 75 female
1 2 Jim Three 85 male
2 3 Arnold Three 55 male
3 4 Krish Four 60 female
4 5 John Four 60 female
5 4 Krish Four 60 female
6 2 Jim Three 85 male
Number to replace in all columns
df=df.replace(85,100) # replace in all columns
Output
id name class1 mark gender
0 1 John Four 75 female
1 2 Max Three 100 male
2 3 Arnold Three 55 male
3 4 Krish Four 60 female
4 5 John Four 60 female
5 4 Krish Four 60 female
6 2 Max Three 100 male
Using List to replace matching string and number
df=df.replace(['John',85],['Jim',100])
Output
id name class1 mark gender
0 1 Jim Four 75 female
1 2 Max Three 100 male
2 3 Arnold Three 55 male
3 4 Krish Four 60 female
4 5 Jim Four 60 female
5 4 Krish Four 60 female
6 2 Max Three 100 male
Using dictionary
df=df.replace({'John':'Jim',85:100}) # using dictionary
Output
id name class1 mark gender
0 1 Jim Four 75 female
1 2 Max Three 100 male
2 3 Arnold Three 55 male
3 4 Krish Four 60 female
4 5 Jim Four 60 female
5 4 Krish Four 60 female
6 2 Max Three 100 male
Replace a list of matching number with one value
df=df.replace([75,85,60],100) # Matching list with one
Output
id name class1 mark gender
0 1 John Four 100 female
1 2 Max Three 100 male
2 3 Arnold Three 55 male
3 4 Krish Four 100 female
4 5 John Four 100 female
5 4 Krish Four 100 female
6 2 Max Three 100 male
id name class1 mark gender
0 1 JimJim 75 female
1 2 Max Three 85 male
2 3 Arnold Three 55 male
3 4 Krish Jim 60 female
4 5 JimJim 60 female
5 4 Krish Jim 60 female
6 2 Max Three 85 male
id name class1 mark gender
0 1 John Four 75 female
1 2 Max Ten 85 male
2 3 Arnold Ten 55 male
3 4 Krish Four 60 female
4 5 John Four 60 female
5 4 Krish Four 60 female
6 2 Max Ten 85 male
Using regular expression
replace starting A or F in all columns
df=df.replace(regex='^[AF]',value='*')
Output
id name class1 mark gender
0 1 John *our 75 female
1 2 Max Three 85 male
2 3 *rnold Three 55 male
3 4 Krish *our 60 female
4 5 John *our 60 female
5 4 Krish *our 60 female
6 2 Max Three 85 male
Starting with M and three char length
df=df.replace(regex={r'^M..$':'foo'})
Output
id name class1 mark gender
0 1 John Four 75 female
1 2 foo Three 85 male
2 3 Arnold Three 55 male
3 4 Krish Four 60 female
4 5 John Four 60 female
5 4 Krish Four 60 female
6 2 foo Three 85 male
replace last two matching chars
df=df.replace(regex={r'hn$':'foo'})
Output
id name class1 mark gender
0 1 Jofoo Four 75 female
1 2 Max Three 85 male
2 3 Arnold Three 55 male
3 4 Krish Four 60 female
4 5 Jofoo Four 60 female
5 4 Krish Four 60 female
6 2 Max Three 85 male
Series
Number of occurrences of pattern in a string.
Returns Series or Index
replacing string
All @ are replaced by #
import pandas as pd
my_dict={'email':['Ravi@example.com','Raju@example.com','Alex@example.com']}
df = pd.DataFrame(data=my_dict)
print(df.email.str.replace('@','#'))