How to add one row in an existing Pandas DataFrame?

In this article, we’ll see how to add a new row of values to an existing dataframe. This can be used when we want to insert a new entry in our data that we might have missed adding earlier. There are different methods to achieve this. Now let’s see with the help of examples how we can do this

Example 1:

We can add a single row using DataFrame.loc. We can add the row at the last in our dataframe. We can get the number of rows using len(DataFrame.index) for determining the position at which we need to add the new row.

filter_none

edit
close

play_arrow

link
brightness_4
code

from IPython.display import display, HTML
  
import pandas as pd
from numpy.random import randint
  
dict = {'Name':['Martha', 'Tim', 'Rob', 'Georgia'],
        'Maths':[87, 91, 97, 95],
        'Science':[83, 99, 84, 76]
       }
  
df = pd.DataFrame(dict)
  
display(df)
  
df.loc[len(df.index)] = ['Amy', 89, 93
  
display(df)

chevron_right


Output:

add-row-to-existing-pandas-dataframe



Example 2:

We can also add a new row using the DataFrame.append() function

filter_none

edit
close

play_arrow

link
brightness_4
code

from IPython.display import display, HTML
  
import pandas as pd
import numpy as np
  
dict = {'Name':['Martha', 'Tim', 'Rob', 'Georgia'],
        'Maths':[87, 91, 97, 95],
        'Science':[83, 99, 84, 76]
       }
  
df = pd.DataFrame(dict)
  
display(df)
  
df2 = {'Name': 'Amy', 'Maths': 89, 'Science': 93}
df = df.append(df2, ignore_index = True)
  
display(df)

chevron_right


Output:

add-row-to-existing-pandas-dataframe

Example 3:

We can also add multiple rows using the pandas.concat() by creating a new dataframe of all the rows that we need to add and then appending this dataframe to the original dataframe.

filter_none

edit
close

play_arrow

link
brightness_4
code

from IPython.display import display, HTML
  
import pandas as pd
import numpy as np
  
dict = {'Name':['Martha', 'Tim', 'Rob', 'Georgia'],
        'Maths':[87, 91, 97, 95],
        'Science':[83, 99, 84, 76]
       }
  
df1 = pd.DataFrame(dict)
display(df1)
  
dict = {'Name':['Amy', 'Maddy'],
        'Maths':[89, 90],
        'Science':[93, 81]
       }
  
df2 = pd.DataFrame(dict)
display(df2)
  
df3 = pd.concat([df1, df2], ignore_index = True)
df3.reset_index()
  
display(df3)

chevron_right


Output:

add-row-to-existing-pandas-dataframe

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.