Friday, December 27, 2024
Google search engine
HomeLanguagesHow to add one row in an existing Pandas DataFrame?

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.

Python3



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)

RELATED ARTICLES

Most Popular

Recent Comments