Python Pandas program to delete records from the DataFrame

In this python pandas program, we will delete records from the DataFrame using pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Delete records for John using  df[df.Name != ‘John’].
  4. It retains all records in the dataframe except for John.
  5. 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("Old Series",df)
df = df[df.Name != 'John']
print("New Series")
print(df)
				
			

Output :

				
					Old Series    Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
3       4  Klaus   33
New DataFrame
   Sr.no.   Name  Age
0       1   Alex   30
2       3  Peter   29
3       4  Klaus   33
				
			

replace the value in a row

add a new column in a DataFrame

Leave a Comment