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

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



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

Here,parameter can be a DataFrame , Series or Dictionary or list of these. Also, ifisthen it will not use indexes.

Let’s see how to use dataframe.append() to add rows in a dataframe.

First create a dataframe using list of tuples i.e.



# 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 dfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'City' , 'Country'], index=['a', 'b', 'c' , 'd' , 'e' , 'f'])

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 row in the dataframe using dataframe.append() and Dictionary

Contents of the dataframe dfObj are,

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 modDfObj = dfObj.append({'Name' : 'Sahil' , 'Age' : 22} , ignore_index=True)

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

It will not modify the existing dataframe object dfObj, 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,New DataFrame’s index is not same as original dataframe becauseis passed asin append() function. Also, for columns which were not present in the dictionaryvalue 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”

Add row in the dataframe using dataframe.append() and Series

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



# Pass a series in append() to append a row in dataframe modDfObj = dfObj.append(pd.Series(['Raju', 21, 'Bangalore', 'India'], index=dfObj.columns ), ignore_index=True)

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 in the dataframe using dataframe.append() and Series

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

We can pass a list of series too in dataframe.append() for appending multiple rows in dataframe.

So, let’s create a list of series with same column names as dataframe i.e.



# List of series listOfSeries = [pd.Series(['Raju', 21, 'Bangalore', 'India'], index=dfObj.columns ) , pd.Series(['Sam', 22, 'Tokyo', 'Japan'], index=dfObj.columns ) , pd.Series(['Rocky', 23, 'Las Vegas', 'US'], index=dfObj.columns ) ]

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

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 7 Sam 22 Tokyo Japan 8 Rocky 23 Las Vegas US

Add a row from one dataframe to other dataframe using dataframe.append()

Now pass this list of series to the append() function i.e.Contents of the dataframe returned are,

Let’s create an another dataframe i.e.



# List of Tuples students = [ ('Rahul', 22, 'Sydeny' , 'Australia') , ('Parul', 23, 'Pune' , 'India') ] #Create a DataFrame object dfObj2 = pd.DataFrame(students, columns = ['Name' , 'Age', 'City' , 'Country'], index=['a', 'b'])

Name Age City Country a Rahul 22 Sydeny Australia b Parul 23 Pune India

# add row at index b from dataframe dfObj2 to dataframe dfObj modDfobj = dfObj.append(dfObj2.loc['b'], ignore_index=True)

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 a row in the dataframe using loc[] & list

Contents of this second dataframe objec dfObj2 are,Now add a row at index ‘b’ from dataframe dfObj2 to dataframe dfObj i.e.Contents of the dataframe returned are,

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

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[]

It will add a new row in dataframe dfObj with index ‘k’ i.e.If dataframe already had any row with index name ‘k’ then this will replace the contents of that row, otherwise it will ad new row.

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

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 k Smriti 26 Bangalore India

import pandas as pd def main(): # 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 dfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'City' , 'Country'], index=['a', 'b', 'c' , 'd' , 'e' , 'f']) print("Original Dataframe" , dfObj, sep='

') print("*****Add row in the dataframe using dataframe.append() ****") # Pass the row elements as key value pairs to append() function modDfObj = dfObj.append({'Name' : 'Sahil' , 'Age' : 22} , ignore_index=True) print("Updated Dataframe" , modDfObj, sep='

') # Pass a series in append() to append a row in dataframe modDfObj = dfObj.append(pd.Series(['Raju', 21, 'Bangalore', 'India'], index=dfObj.columns ), ignore_index=True) print("Updated Dataframe" , modDfObj, sep='

') print("**** Add multiple rows in the dataframe using dataframe.append() and Series ****") # List of series listOfSeries = [pd.Series(['Raju', 21, 'Bangalore', 'India'], index=dfObj.columns ) , pd.Series(['Sam', 22, 'Tokyo', 'Japan'], index=dfObj.columns ) , pd.Series(['Rocky', 23, 'Las Vegas', 'US'], index=dfObj.columns ) ] # Pass a list of series to the append() to add multiple rows modDfObj = dfObj.append(listOfSeries , ignore_index=True) print("Updated Dataframe" , modDfObj, sep='

') print("*****Add a row from one dataframe to other dataframe ****") # Create an another dataframe # List of Tuples students = [ ('Rahul', 22, 'Sydeny' , 'Australia') , ('Parul', 23, 'Pune' , 'India') ] #Create a DataFrame object dfObj2 = pd.DataFrame(students, columns = ['Name' , 'Age', 'City' , 'Country'], index=['a', 'b']) print("Another Dataframe" , dfObj2, sep='

') # add row at index b from dataframe dfObj2 to dataframe dfObj modDfObj = dfObj.append(dfObj2.loc['b'], ignore_index=True) print("Updated Dataframe" , modDfObj, sep='

') print("*****Add a row in the dataframe using loc[] ****") # Add a new row at index k with values provided in list dfObj.loc['k'] = ['Smriti', 26, 'Bangalore', 'India'] print("Updated Dataframe" , dfObj, sep='

') print("*****Add a row in the dataframe at index position using iloc[] ****") # Add a new row at index position 2 with values provided in list dfObj.iloc[2] = ['Smriti', 26, 'Bangalore', 'India'] print("Updated Dataframe" , dfObj, sep='

') if __name__ == '__main__': main()

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 *****Add row in the dataframe using dataframe.append() **** Updated 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 Updated 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 in the dataframe using dataframe.append() and Series **** Updated 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 7 Sam 22 Tokyo Japan 8 Rocky 23 Las Vegas US *****Add a row from one dataframe to other dataframe **** Another Dataframe Name Age City Country a Rahul 22 Sydeny Australia b Parul 23 Pune India Updated 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 a row in the dataframe using loc[] **** Updated 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[] **** Updated 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 k Smriti 26 Bangalore India

It will replace the row at index position 2 in dataframe dfObj with new row i.e.