How to add new row in pandas DataFrame || Pandas DataFrame Function DataFrame.loc(), DataFrame.append(), and DataFrame.append()

 How to add new row in Pandas DataFrame

We can easily add a new row in the existing Data Frame. There are various methods to add a new row in Data Frame.

#1. Method:- 

  We can add a new row using the DataFrame.loc() function. This function will add the row at the end. We will use another function len(DataFrame.index) to get the position at which we want to add a new row.

import pandas as pd
dict= { ‘Name’ : [‘Raj’, “Ajay”, “Rahul”], ‘Percentage’ : [78,80,75] }
df= pd. DataFrame(dict)
print(df)
print(“After adding a new row”)
df.loc[ len(df.index) ] = [ “Vikas”, 70]
print(df)

Its output will be

        Name            Percentage
一一一一一一一一一一一
0        Raj                    78
1        Ajay                  80
2        Rahul                75
After adding a new row
        Name            Percentage
一一一一一一一一一一一
0        Raj                    78
1        Ajay                  80
2        Rahul                75
3        Vikas                70

#2. Method:- 

 We can add a new row using the DataFrame.append() function.This function will append the New Data Frame row at the end of the existing DataFrame.

import pandas as pd
import numpy as np
dict= { ‘Name’ : [‘Raj’, “Ajay”, “Rahul”], ‘Percentage’ : [78,80,75] }
df= pd. DataFrame(dict)
print(df)
print(“After adding a new row”)
df2= { “Name” : [“Vikas”], “Percentage” : 70}
df=df.append(df2, ignore_index= True)
print(df)

Its output will be

        Name            Percentage
一一一一一一一一一一一
0        Raj                    78
1        Ajay                  80
2        Rahul                75
After adding a new row
        Name            Percentage
一一一一一一一一一一一
0        Raj                    78
1        Ajay                  80
2        Rahul                75
3        Vikas                70

#3. Method:- 

 We can also add multiple number of news row using the pandas.concat() function.This function will append the New Data Frame rows at the end of the existing DataFrame, and create a new DataFrame.

import pandas as pd
import numpy as np
dict= { “Name” : [‘Raj’, “Ajay”, “Rahul”], “Percentage” : [78,80,75] }
df1= pd. DataFrame(dict)
print(df1)
dict= { “Name” : [“Keshu”, “Vipin” ], “Percentage” : [77,85 ] }
df2= pd. DataFrame(dict)
print(df2)
print(“After adding “)
df3 = pd.concat( [df1, df2 ], ignore_index= True)
df3.reset_index()
print(df3)

Its output will be

        Name            Percentage
一一一一一一一一一一一
0        Raj                    78
1        Ajay                  80
2        Rahul                75
        Name            Percentage
一一一一一一一一一一一
0        Keshu               77
1        Vipin                85
After adding
        Name            Percentage
一一一一一一一一一一一
0        Raj                    78
1        Ajay                  80
2        Rahul                75
3        Keshu               77
4        Vipin                 85
 If you like my post please share it with your friends by simply clicking the below social media button, and don’t forget to comment so we will update ourselves.
Share in a Single Click

Leave a Reply

Your email address will not be published. Required fields are marked *