Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Stack Multiple Pandas DataFrames?

  • Last Updated : 22 Nov, 2021

In this article, we will see how to stack Multiple pandas dataframe. Stacking means appending the dataframe rows to the second dataframe and so on. If there are 4 dataframes, then after stacking the result will be a single dataframe with an order of dataframe1,dataframe2,dataframe3,dataframe4

Method 1: Using concat() method 

This method will stack the rows of the pandas dataframes in the order they give.

Syntax: pandas.concat([first_dataframe,second_dataframe,third_dataframe,………….,last_dataframe], ignore_index=True,axis)

Parameters:

  • dataframes are the input dataframes to be stacked
  • ignore_index is used to ignore the index values of the input dataframes
  • axis=0 specifies vertical stacking
  • axis=1 specifies horizontal stacking

Note: If the ignore_index parameter is not set to true means then it will take the given indexes which lead to the wrong stacking of the dataframes

Example 1: Python program to stack two dataframes  vertically

Python3




# import pandas module
import pandas as pd
  
# create first dataframe
data1 = pd.DataFrame({'name': ['sravan', 'bobby',
                               'ojaswi', 'rohith'
                               'gnanesh'],
                      'subjects': ['java', 'python',
                                   'php', 'java', '.NET']})
  
# create second dataframe
data2 = pd.DataFrame({'name': ['gopi', 'harsha', 'ravi',
                               'uma', 'deepika'],
                      'subjects': ['c/c++', 'html/css',
                                   'dbms', 'java', 'IOT']})
  
# stack the two DataFrames
pd.concat([data1, data2], ignore_index=True, axis=0)

Output:

Example 2: Python code to stack multiple dataframes vertically

Python3




# import pandas module
import pandas as pd
  
# create first dataframe
data1 = pd.DataFrame({'name': ['sravan', 'bobby', 'ojaswi',
                               'rohith', 'gnanesh'],
                      'subjects': ['java', 'python', 'php',
                                   'java', '.NET']})
  
# create second dataframe
data2 = pd.DataFrame({'name': ['gopi', 'harsha', 'ravi',
                               'uma', 'deepika'], 
                      'subjects': ['c/c++', 'html/css',
                                   'dbms', 'java', 'IOT']})
  
# create third dataframe
data3 = pd.DataFrame(
    {'name': ['ragini', 'latha'], 'subjects': ['java', 'python']})
  
# create forth dataframe
data4 = pd.DataFrame(
    {'name': ['gowri', 'jyothika'], 'subjects': ['java', 'IOT']})
  
# stack the four DataFrames
pd.concat([data1, data2, data3, data4], ignore_index=True,axis=0)

Output:

Example 3: Python program to stack multiple dataframes horizontally

Python3




# import pandas module
import pandas as pd
  
# create first dataframe
data1 = pd.DataFrame({'name': ['sravan', 'bobby', 'ojaswi'
                               'rohith', 'gnanesh'], 
                      'subjects': ['java', 'python',
                                   'php', 'java', '.NET']})
  
# create second dataframe
data2 = pd.DataFrame({'name': ['gopi', 'harsha', 'ravi',
                               'uma', 'deepika'], 
                      'subjects': ['c/c++', 'html/css',
                                   'dbms', 'java', 'IOT']})
  
# create third dataframe
data3 = pd.DataFrame(
    {'name': ['ragini', 'latha'], 'subjects': ['java', 'python']})
  
# create forth dataframe
data4 = pd.DataFrame(
    {'name': ['gowri', 'jyothika'], 'subjects': ['java', 'IOT']})
  
# stack the four DataFrames horizontally
pd.concat([data1, data2, data3, data4], axis=1, ignore_index=True)

Output:

Method 2: Using append() method

append() method is used to append the dataframes after the given dataframe.

Syntax: first_dataframe.append([second_dataframe,…,last_dataframe],ignore_index=True)

Example: Python program to stack multiple dataframes using append() method

Python3




# import pandas module
import pandas as pd
  
# create first dataframe
data1 = pd.DataFrame({'name': ['sravan', 'bobby', 'ojaswi',
                               'rohith', 'gnanesh'],
                      'subjects': ['java', 'python', 'php',
                                   'java', '.NET']})
  
# create second dataframe
data2 = pd.DataFrame({'name': ['gopi', 'harsha', 'ravi',
                               'uma', 'deepika'],
                      'subjects': [ 'c/c++', 'html/css',
                                   'dbms', 'java', 'IOT']})
  
# create third dataframe
data3 = pd.DataFrame(
    {'name': ['ragini', 'latha'], 'subjects': ['java', 'python']})
  
# create forth dataframe
data4 = pd.DataFrame(
    {'name': ['gowri', 'jyothika'], 'subjects': ['java', 'IOT']})
  
# stack the four DataFrames using append()
data1.append([data2, data3, data4], ignore_index=True)

Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!