Python Pandas program to add a new row in the DataFrame

In this python pandas program, we will add a new row 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. Create a new DataFrame containing the new row.
  4. Now add that row to the original DataFrame using df.append(df1, ignore_index=True).
  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("Original Series: ")
print(df)
df1 = {'Sr.no.':5,'Name':'Jason','Age':28}
df = df.append(df1, ignore_index=True)
print("New Series: ")
print(df)
				
			

Output :

				
					Original Series: 
   Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
3       4  Klaus   33
New Series: 
   Sr.no.   Name  Age
0       1   Alex   30
1       2   John   27
2       3  Peter   29
3       4  Klaus   33
4       5  Jason   28
				
			

calculate the sum of age column

sort the DataFrame first by ‘name’ in ascending order

Leave a Comment