Python Pandas program to replace a DataFrame value

In this python pandas program, we will replace a DataFrame value using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Replace John with Jim in the DataFrame using df[‘Name’].replace(‘John’,’Jim’).
  4. Print the output.
				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33]}
df = pd.DataFrame(d)
print(df)
print("Change the age of John to Jim:")
df['Name'] = df['Name'].replace('John','Jim')
print(df)
				
			

Output :

				
					0   Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
3       4  Klaus   33
Change the age of John to Jim:
   Sr.no.   Name  Age
0       1   Alex   30
1       2    Jim   27
2       3  Peter   29
3       4  Klaus   33
				
			

sort the DataFrame by ‘age’ column in ascending order

delete a record from the DataFrame

Leave a Comment