df1: Column 1 Column 2 Column 3 Row 1 1 2 3 Row 2 4 5 6 Row 3 7 8 9 df2: Column 1 Column 2 Column 3 Row 4 10 11 12 Row 5 13 14 15 df3 after concatenation: Column 1 Column 2 Column 3 Row 1 1 2 3 Row 2 4 5 6 Row 3 7 8 9 Row 4 10 11 12 Row 5 13 14 15
Here, df1 and df2 are concatenated along the rows. So, the axis parameter in the pandas.concat() function is 0.
How to concatenate two DataFrames along the columns using Python pandas?
We can use the following Python code to concatenate two DataFrames along the columns using Python pandas.
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 1", "Row 2", "Row 3"], columns=["Column 4", "Column 5"]) print("df2: \n", df2) df3 = pandas.concat([df1, df2], axis=1) print("df3 after concatenation: \n", df3)
The output of the above program will be:






0 Comments