How to Stack Multiple Pandas DataFrames? - GeeksforGeeks

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
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
Python3
# import pandas moduleimport pandas as pd  # create first dataframedata1 = pd.DataFrame({'name': ['sravan', 'bobby',                               'ojaswi', 'rohith',                                'gnanesh'],                      'subjects': ['java', 'python',                                   'php', 'java', '.NET']})  # create second dataframedata2 = pd.DataFrame({'name': ['gopi', 'harsha', 'ravi',                               'uma', 'deepika'],                      'subjects': ['c/c++', 'html/css',                                   'dbms', 'java', 'IOT']})  # stack the two DataFramespd.concat([data1, data2], ignore_index=True, axis=0)
Output:
Python3
# import pandas moduleimport pandas as pd  # create first dataframedata1 = pd.DataFrame({'name': ['sravan', 'bobby', 'ojaswi',                               'rohith', 'gnanesh'],                      'subjects': ['java', 'python', 'php',                                   'java', '.NET']})  # create second dataframedata2 = pd.DataFrame({'name': ['gopi', 'harsha', 'ravi',                               'uma', 'deepika'],                       'subjects': ['c/c++', 'html/css',                                   'dbms', 'java', 'IOT']})  # create third dataframedata3 = pd.DataFrame(    {'name': ['ragini', 'latha'], 'subjects': ['java', 'python']})  # create forth dataframedata4 = pd.DataFrame(    {'name': ['gowri', 'jyothika'], 'subjects': ['java', 'IOT']})  # stack the four DataFramespd.concat([data1, data2, data3, data4], ignore_index=True,axis=0)
Output:
Python3
# import pandas moduleimport pandas as pd  # create first dataframedata1 = pd.DataFrame({'name': ['sravan', 'bobby', 'ojaswi',                                'rohith', 'gnanesh'],                       'subjects': ['java', 'python',                                   'php', 'java', '.NET']})  # create second dataframedata2 = pd.DataFrame({'name': ['gopi', 'harsha', 'ravi',                               'uma', 'deepika'],                       'subjects': ['c/c++', 'html/css',                                   'dbms', 'java', 'IOT']})  # create third dataframedata3 = pd.DataFrame(    {'name': ['ragini', 'latha'], 'subjects': ['java', 'python']})  # create forth dataframedata4 = pd.DataFrame(    {'name': ['gowri', 'jyothika'], 'subjects': ['java', 'IOT']})  # stack the four DataFrames horizontallypd.concat([data1, data2, data3, data4], axis=1, ignore_index=True)
Output:
append() method is used to append the dataframes after the given dataframe.
Syntax: first_dataframe.append([second_dataframe,...,last_dataframe],ignore_index=True)
Python3
# import pandas moduleimport pandas as pd  # create first dataframedata1 = pd.DataFrame({'name': ['sravan', 'bobby', 'ojaswi',                               'rohith', 'gnanesh'],                      'subjects': ['java', 'python', 'php',                                   'java', '.NET']})  # create second dataframedata2 = pd.DataFrame({'name': ['gopi', 'harsha', 'ravi',                               'uma', 'deepika'],                      'subjects': [ 'c/c++', 'html/css',                                   'dbms', 'java', 'IOT']})  # create third dataframedata3 = pd.DataFrame(    {'name': ['ragini', 'latha'], 'subjects': ['java', 'python']})  # create forth dataframedata4 = pd.DataFrame(    {'name': ['gowri', 'jyothika'], 'subjects': ['java', 'IOT']})  # stack the four DataFrames using append()data1.append([data2, data3, data4], ignore_index=True)
Output:
Picked
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Selecting rows in pandas DataFrame based on conditions
Create a directory in Python
Python | Pandas dataframe.groupby()