In Python, we can use the pandas.concat() function to concatenate two DataFrames. We can concatenate two DataFrames along the rows or along the columns. In this article, we will discuss:
- How to concatenate two DataFrames along the rows using Python pandas?
- How to concatenate two DataFrames along the columns using Python pandas?
How to concatenate two DataFrames along the rows using Python pandas?
We can use the following code to concatenate two DataFrames along the rows.
import pandas list1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] df1 = pandas.DataFrame(list1, index=["Row 1", "Row 2", "Row 3"], columns=["Column 1", "Column 2", "Column 3"]) print("df1: \n", df1) list2 = [[10, 11, 12], [13, 14, 15]] df2 = pandas.DataFrame(list2, index=["Row 4", "Row 5"], columns=["Column 1", "Column 2", "Column 3"]) print("df2: \n", df2) df3 = pandas.concat([df1, df2], axis=0) print("df3 after concatenation: \n", df3)
The output of the above program is:






0 Comments