8

Python Pandas : How to add rows in a DataFrame using dataframe.append() & lo...

 3 years ago
source link: https://thispointer.com/python-pandas-how-to-add-rows-in-a-dataframe-using-dataframe-append-loc-iloc/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to add rows in a DataFrame using dataframe.append() & loc[] , iloc[] – thispointer.com

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)
DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=None)

Here, ‘other’ parameter can be a DataFrame , Series or Dictionary or list of these. Also, if ignore_index is True then 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'])
# 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']) 

Contents of the dataframe dfObj are,
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
    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

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)
# Pass the row elements as key value pairs to append() function 
modDfObj = dfObj.append({'Name' : 'Sahil' , 'Age' : 22} , ignore_index=True)

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,
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
    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”

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)
# 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)

While creating a series object we pass the index names same as index names of dataframe. Contents of the dataframe returned are,
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
    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

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 ) ]
# 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 ) ]


Now pass this list of series to the append() function i.e.
# Pass a list of series to the append() to add multiple rows
modDfObj = dfObj.append(listOfSeries , ignore_index=True)
# Pass a list of series to the append() to add multiple rows
modDfObj = dfObj.append(listOfSeries , ignore_index=True)

Contents of the dataframe returned are,
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
    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()

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'])
# 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']) 

Contents of this second dataframe objec dfObj2 are,
Name Age City Country
a Rahul 22 Sydeny Australia
b Parul 23 Pune India
    Name  Age    City    Country
a  Rahul   22  Sydeny  Australia
b  Parul   23    Pune      India

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

Contents of the dataframe returned are,
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
    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

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

It will add a new row in dataframe dfObj with index ‘k’ i.e.
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
     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

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 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']
# Add a new row at index position 2 with values provided in list
dfObj.iloc[2] = ['Smriti', 26, 'Bangalore', 'India']

It will replace the row at index position 2 in dataframe dfObj with new row i.e.
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
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

Complete example is as follows,
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='\n')
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='\n')
# 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='\n')
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='\n')
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='\n')
# 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='\n')
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='\n')
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='\n')
if __name__ == '__main__':
main()
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='\n')
    

    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='\n')

    # 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='\n')

    
    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='\n')
    
    
    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='\n')

    # 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='\n')
    
    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='\n')
    
    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='\n')
    
    

    
if __name__ == '__main__':
    main()


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
*****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
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

Join a list of 2000+ Programmers for latest Tips & Tutorials


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK