Python Pandas program to change the observation in the DataFrame

In this python pandas program, we will change the observation in the DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Change the age of John to 24 in the DataFrame using df[‘Age’].replace(27,24).
  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 24:")
df['Age'] = df['Age'].replace(27,24)
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 24:
   Sr.no.   Name  Age
0       1   Alex   30
1       2   John   24
2       3  Peter   29
3       4  Klaus   33
				
			

print the names who’s age is between 25-30 using Pandas

calculate the sum of age column

Leave a Comment