Replace a value
Write a pandas program to replace John with Jim.
name age 0 Alex 30 1 Jim 27 2 Peter 29 3 Klaus 33
Approach
replace()on the column.
Solution
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter", "Klaus"], "age": [30, 27, 29, 33]})df["name"] = df["name"].replace("John", "Jim")print(df)name age 0 Alex 30 1 Jim 27 2 Peter 29 3 Klaus 33
Learn the concept
New to this topic? Read the lesson, then come back.