Row bind in python pandas – Append or concatenate rows in python pandas

Row bind in python pandas – In this tutorial we will learn how to concatenate rows to the python pandas dataframe  with append() Function and concat() Function i.e. how to row bind two data frames in python pandas with an example.

Row binding is pictographically shown below

rowbind in python pandas

 

Create dataframe 1:

import pandas as pd
import numpy as np

#Create a DataFrame
d = {
    'Name':['Alisa','Bobby','Cathrine','Madonna','Rocky','Sebastian','Jaqluine',
   'Rahul','David'],
   'Score1':[62,47,55,74,31,77,85,63,42],
   'Score2':[89,87,67,55,47,72,76,79,44],
   'Score3':[56,86,77,45,73,62,74,89,71]}

df1 = pd.DataFrame(d)
df1

so the dataframe 1 will be

Append or concatenate rows in python pandas – Row bind in python pandas

 

Create dataframe 2:


import pandas as pd
import numpy as np

# create dataframe - df2
d = {
    'Name':['Andrew','Ajay','Teresa'],
   'Score1':[32,71,57],
   'Score2':[92,99,69],
   'Score3':[67,97,68]}

df2 = pd.DataFrame(d)
df2

and the dataframe 2 will be

Append or concatenate rows in python pandas – Row bind in python pandas

 

Method 1: Row bind or concatenate two dataframes in pandas :

Now lets concatenate or row bind two dataframes df1 and df2


pd.concat([df1,df2])

so the resultant row binded dataframe will be

Append or concatenate rows in python pandas – Row bind in python pandas 3

Method 2: Row bind or concatenate two dataframes in pandas:

Now lets concatenate or row bind two dataframes df1 and df2 with append method


df1.append(df2)

so the resultant dataframe will be

Append or concatenate rows in python pandas – Row bind in python pandas 4

Concatenate or append rows of dataframe with different column names

Now Lets  create dataframe 3


import pandas as pd
import numpy as np

# create dataframe – df3
d = {
    'Name':['Jack','danny','vishwa'],
   'Score1':[32,71,70],
   'Score4':[72,91,89],
   'Score5':[57,72,78]}

df3 = pd.DataFrame(d)
df3

Append or concatenate rows in python pandas – Row bind in python pandas 5

 

Concatenate the df1 and df3 whose column names are different:


pd.concat([df1,df3])

result:

Append or concatenate rows in python pandas – Row bind in python pandas 6

 

Concatenate the df1 and df3 with append function


df1.append(df3)

result:

Append or concatenate rows in python pandas – Row bind in python pandas 7

previous-small Row bind in python pandas - Append or concatenate rows in python pandas                                                                                                           next_small Row bind in python pandas - Append or concatenate rows in python pandas

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.