Add Row to Dataframe in Pandas

In this article, we will discuss how to add / append a single or multiple rows in a dataframe using dataframe.append() or loc & iloc.

Table of Contents

Overview of pandas dataframe append()

Pandas Dataframe provides a function dataframe.append() to add rows to a dataframe i.e.

DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=None)

Here, the ‘other’ parameter can be a DataFrame or Series or Dictionary or list of these. Also, if ignore_index is True then it will not use indexes.

Examples of adding row to the dataframe

Suppose we have a dataframe df, whose contents are as follows,

    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi      India
c  Vikas   31     Mumbai      India
d  Neelu   32  Bangalore      India
e   John   16   New York         US
f   Mike   17  las vegas         US

Add dictionary as a row to dataframe

In dataframe.append() we can pass a dictionary of key value pairs i.e.

  • key = Column name
  • Value = Value at that column in new row

Let’s add a new row in above dataframe by passing dictionary i.e.

# Pass the row elements as key value pairs to append() function 
mod_df = df.append({'Name' : 'Sahil',
                    'Age' : 22} , 
                    ignore_index=True)

print('Modified Dataframe')
print(mod_df)

It will not modify the existing dataframe object mod_df, it will return a new dataframe containing copy of contents of existing dataframe and with a new row appended at it’s end. Contents of the dataframe returned are,

Modified Dataframe
    Name  Age       City    Country
0   jack   34     Sydeny  Australia
1   Riti   30      Delhi      India
2  Vikas   31     Mumbai      India
3  Neelu   32  Bangalore      India
4   John   16   New York         US
5   Mike   17  las vegas         US
6  Sahil   22        NaN        NaN

New DataFrame’s index is not same as original dataframe because ignore_index is passed as True in append() function. Also, for columns which were not present in the dictionary NaN value is added.

Passing ignore_index=True is necessary while passing dictionary or series otherwise following TypeError error will come i.e.

“TypeError: Can only append a Series if ignore_index=True or if the Series has a name”

Complete example to add a dictionary as row to the dataframe is as follows,

import pandas as pd

# List of Tuples
students = [ ('jack', 34, 'Sydeny' , 'Australia') ,
             ('Riti', 30, 'Delhi' , 'India' ) ,
             ('Vikas', 31, 'Mumbai' , 'India' ) ,
             ('Neelu', 32, 'Bangalore' , 'India' ) ,
             ('John', 16, 'New York' , 'US') ,
             ('Mike', 17, 'las vegas' , 'US')  ]

#Create a DataFrame object
df = pd.DataFrame(  students, 
                    columns = ['Name' , 'Age', 'City' , 'Country'],
                    index=['a', 'b', 'c' , 'd' , 'e' , 'f']) 

print('Original Dataframe')
print(df)

# Pass the row elements as key value pairs to append() function 
mod_df = df.append({'Name' : 'Sahil',
                    'Age' : 22} , 
                    ignore_index=True)

print('Modified Dataframe')
print(mod_df)

Output:

Original Dataframe
    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi      India
c  Vikas   31     Mumbai      India
d  Neelu   32  Bangalore      India
e   John   16   New York         US
f   Mike   17  las vegas         US
Modified Dataframe
    Name  Age       City    Country
0   jack   34     Sydeny  Australia
1   Riti   30      Delhi      India
2  Vikas   31     Mumbai      India
3  Neelu   32  Bangalore      India
4   John   16   New York         US
5   Mike   17  las vegas         US
6  Sahil   22        NaN        NaN

Add Series as a row in the dataframe

We can also pass a series object to the append() function to append a new row to the dataframe i.e.

# A series object with same index as dataframe
series_obj = pd.Series( ['Raju', 21, 'Bangalore', 'India'], 
                        index=dfObj.columns )

# Add a series as a row to the dataframe  
mod_df = dfObj.append(  series_obj,
                        ignore_index=True)

While creating a series object we passed the index names same as index of dataframe. Contents of the dataframe returned are,

Modified Dataframe
    Name  Age       City    Country
0   jack   34     Sydeny  Australia
1   Riti   30      Delhi      India
2  Vikas   31     Mumbai      India
3  Neelu   32  Bangalore      India
4   John   16   New York         US
5   Mike   17  las vegas         US
6   Raju   21  Bangalore      India

Checkout the complete example to a append a series as row to dataframe,

import pandas as pd

# List of Tuples
students = [ ('jack', 34, 'Sydeny' , 'Australia') ,
             ('Riti', 30, 'Delhi' , 'India' ) ,
             ('Vikas', 31, 'Mumbai' , 'India' ) ,
             ('Neelu', 32, 'Bangalore' , 'India' ) ,
             ('John', 16, 'New York' , 'US') ,
             ('Mike', 17, 'las vegas' , 'US')  ]

#Create a DataFrame object
df = pd.DataFrame(  students, 
                    columns = ['Name' , 'Age', 'City' , 'Country'],
                    index=['a', 'b', 'c' , 'd' , 'e' , 'f']) 

print('Original Dataframe')
print(df)

# A series object with same index as dataframe
series_obj = pd.Series( ['Raju', 21, 'Bangalore', 'India'], 
                        index=df.columns )

# Add a series as a row to the dataframe  
mod_df = df.append(  series_obj,
                        ignore_index=True)

print('Modified Dataframe')
print(mod_df)

Output:

Original Dataframe
    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi      India
c  Vikas   31     Mumbai      India
d  Neelu   32  Bangalore      India
e   John   16   New York         US
f   Mike   17  las vegas         US
Modified Dataframe
    Name  Age       City    Country
0   jack   34     Sydeny  Australia
1   Riti   30      Delhi      India
2  Vikas   31     Mumbai      India
3  Neelu   32  Bangalore      India
4   John   16   New York         US
5   Mike   17  las vegas         US
6   Raju   21  Bangalore      India

Add multiple rows to pandas dataframe

We can pass a list of series too in the dataframe.append() for appending multiple rows in dataframe. For example, we can create a list of series with same column names as dataframe i.e.

# List of series with same Index as datframe
listOfSeries = [pd.Series(['Luke', 21, 'Bangalore', 'India'], index=df.columns ) ,
                pd.Series(['Sam', 22, 'Tokyo', 'Japan'], index=df.columns ) ,
                pd.Series(['Rocky', 23, 'Las Vegas', 'US'], index=df.columns ) ]

Now pass this list of series to the append() function i.e.

# Pass a list of series to the append() to add 
# multiple rows to dataframe
mod_df = df.append(  listOfSeries,
                        ignore_index=True)

Contents of the dataframe returned are,

Modified Dataframe
    Name  Age       City    Country
0   jack   34     Sydeny  Australia
1   Riti   30      Delhi      India
2  Vikas   31     Mumbai      India
3  Neelu   32  Bangalore      India
4   John   16   New York         US
5   Mike   17  las vegas         US
6   Luke   21  Bangalore      India
7    Sam   22      Tokyo      Japan
8  Rocky   23  Las Vegas         US

Complete example to add multiple rows to dataframe is as follows,

import pandas as pd

# List of Tuples
students = [ ('jack', 34, 'Sydeny' , 'Australia') ,
             ('Riti', 30, 'Delhi' , 'India' ) ,
             ('Vikas', 31, 'Mumbai' , 'India' ) ,
             ('Neelu', 32, 'Bangalore' , 'India' ) ,
             ('John', 16, 'New York' , 'US') ,
             ('Mike', 17, 'las vegas' , 'US')  ]

#Create a DataFrame object
df = pd.DataFrame(  students, 
                    columns = ['Name' , 'Age', 'City' , 'Country'],
                    index=['a', 'b', 'c' , 'd' , 'e' , 'f']) 

print('Original Dataframe')
print(df)

# List of series with same Index as datframe
listOfSeries = [pd.Series(['Luke', 21, 'Bangalore', 'India'], index=df.columns ) ,
                pd.Series(['Sam', 22, 'Tokyo', 'Japan'], index=df.columns ) ,
                pd.Series(['Rocky', 23, 'Las Vegas', 'US'], index=df.columns ) ]

# Pass a list of series to the append() to add 
# multiple rows to dataframe
mod_df = df.append(  listOfSeries,
                        ignore_index=True)

print('Modified Dataframe')
print(mod_df)

Output

Original Dataframe
    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi      India
c  Vikas   31     Mumbai      India
d  Neelu   32  Bangalore      India
e   John   16   New York         US
f   Mike   17  las vegas         US
Modified Dataframe
    Name  Age       City    Country
0   jack   34     Sydeny  Australia
1   Riti   30      Delhi      India
2  Vikas   31     Mumbai      India
3  Neelu   32  Bangalore      India
4   John   16   New York         US
5   Mike   17  las vegas         US
6   Luke   21  Bangalore      India
7    Sam   22      Tokyo      Japan
8  Rocky   23  Las Vegas         US

Add row from one dataframe to another dataframe

We can select a row from dataframe by its name using loc[] attribute and the pass the selected row as an argument to the append() function. It will add the that row to the another dataframe. Let’s see an example where we will select a row with index label ‘b’ and append it to another dataframe using append(). For example,

import pandas as pd

# List of Tuples
students = [ ('jack', 34, 'Sydeny' , 'Australia') ,
             ('Riti', 30, 'Delhi' , 'India' ) ,
             ('Vikas', 31, 'Mumbai' , 'India' ) ,
             ('Neelu', 32, 'Bangalore' , 'India' ) ,
             ('John', 16, 'New York' , 'US') ,
             ('Mike', 17, 'las vegas' , 'US')  ]

#Create a DataFrame object
df = pd.DataFrame(  students, 
                    columns = ['Name' , 'Age', 'City' , 'Country'],
                    index=['a', 'b', 'c' , 'd' , 'e' , 'f']) 

print('First Dataframe')
print(df)

# List of Tuples
students = [ ('Rahul', 22, 'Sydeny' , 'Australia') ,
             ('Parul', 23, 'Pune' , 'India')  ]

#Create a DataFrame object
df_2 = pd.DataFrame(students, 
                    columns = ['Name' , 'Age', 'City' , 'Country'],
                    index=['a', 'b']) 

print('Second Dataframe')
print(df_2)

# add row at index b from dataframe df_2 to dataframe df
mod_df = df.append(df_2.loc['b'], 
                   ignore_index=True)

print('Modified Dataframe')
print(mod_df)

Output

First Dataframe
    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi      India
c  Vikas   31     Mumbai      India
d  Neelu   32  Bangalore      India
e   John   16   New York         US
f   Mike   17  las vegas         US
Second Dataframe
    Name  Age    City    Country
a  Rahul   22  Sydeny  Australia
b  Parul   23    Pune      India
Modified Dataframe
    Name  Age       City    Country
0   jack   34     Sydeny  Australia
1   Riti   30      Delhi      India
2  Vikas   31     Mumbai      India
3  Neelu   32  Bangalore      India
4   John   16   New York         US
5   Mike   17  las vegas         US
6  Parul   23       Pune      India

Add list as a row to pandas dataframe using loc[]

Adding a list as a row to the dataframe in pandas is very simple and easy. We can just pass the new index label in loc[] attribute and assign list object to it. For example,

# Add a new row at index k with values provided in list
df.loc['k'] = ['Smriti', 26, 'Bangalore', 'India']

It will append a new row to the dataframe with index label ‘k’. Let’s see a complete example to append a list as row to the dataframe,

import pandas as pd

# List of Tuples
students = [ ('jack', 34, 'Sydeny' , 'Australia') ,
             ('Riti', 30, 'Delhi' , 'India' ) ,
             ('Vikas', 31, 'Mumbai' , 'India' ) ,
             ('Neelu', 32, 'Bangalore' , 'India' ) ,
             ('John', 16, 'New York' , 'US') ,
             ('Mike', 17, 'las vegas' , 'US')  ]

#Create a DataFrame object
df = pd.DataFrame(  students, 
                    columns = ['Name' , 'Age', 'City' , 'Country'],
                    index=['a', 'b', 'c' , 'd' , 'e' , 'f']) 

print('Original Dataframe')
print(df)

# Add a new row at index k with values provided in list
df.loc['k'] = ['Smriti', 26, 'Bangalore', 'India']

print('Modified Dataframe')
print(df)

Output:

Original Dataframe
    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi      India
c  Vikas   31     Mumbai      India
d  Neelu   32  Bangalore      India
e   John   16   New York         US
f   Mike   17  las vegas         US
Modified Dataframe
     Name  Age       City    Country
a    jack   34     Sydeny  Australia
b    Riti   30      Delhi      India
c   Vikas   31     Mumbai      India
d   Neelu   32  Bangalore      India
e    John   16   New York         US
f    Mike   17  las vegas         US
k  Smriti   26  Bangalore      India

Add a row in the dataframe at index position using iloc[]

We can add a row at specific position too in the dataframe using iloc[] attribute. Checkout the example, where we will add a list as the 3rd row the dataframe. For example,

import pandas as pd

# List of Tuples
students = [ ('jack', 34, 'Sydeny' , 'Australia') ,
             ('Riti', 30, 'Delhi' , 'India' ) ,
             ('Vikas', 31, 'Mumbai' , 'India' ) ,
             ('Neelu', 32, 'Bangalore' , 'India' ) ,
             ('John', 16, 'New York' , 'US') ,
             ('Mike', 17, 'las vegas' , 'US')  ]

#Create a DataFrame object
df = pd.DataFrame(  students, 
                    columns = ['Name' , 'Age', 'City' , 'Country'],
                    index=['a', 'b', 'c' , 'd' , 'e' , 'f']) 

print('Original Dataframe')
print(df)

# Add a new row at index position 2 with values provided in list
df.iloc[2] = ['Smriti', 26, 'Bangalore', 'India']

print('Modified Dataframe')
print(df)

Output:

Original Dataframe
    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi      India
c  Vikas   31     Mumbai      India
d  Neelu   32  Bangalore      India
e   John   16   New York         US
f   Mike   17  las vegas         US
Modified Dataframe
     Name  Age       City    Country
a    jack   34     Sydeny  Australia
b    Riti   30      Delhi      India
c  Smriti   26  Bangalore      India
d   Neelu   32  Bangalore      India
e    John   16   New York         US
f    Mike   17  las vegas         US

Summary:

We learned about different ways to add / append rows to the dataframe in pandas.

4 thoughts on “Add Row to Dataframe in Pandas”

  1. thank you, my friend – this was such a helpful post!
    it answered my exact question about adding using iloc and what order the columns would be, and it also showed me a few other things i didn’t know.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top