Concatenate Two or More Pandas DataFrame

Concatenation of two or more data frames in pandas can be done using concat() function of pandas package , which allows you to concatenate two or more DataFrames . This can be done in both ways i.e row wise and column wise .

 

Concatenating DataFrames along row

The below code is for concatenating two dataframes row wise

Program :

import pandas as pd

df1 = pd.DataFrame({ #creating a dataframe
    'A': [1, 2],
    'B': [3, 4]
})

df2 = pd.DataFrame({ #creating another dataframe 
    'A': [5, 6],
    'B': [7, 8]
})

result = pd.concat([df1, df2], axis=0)  # Concatenate along rows (axis=0)
print(result)


Output:

  A B
0 1 3
1 2 4
0 5 7
1 6 8

Here, we can understand that the two dataframes are concatenated row wise . This is by specifing axis = 0 , by default axis is always zero.

This means that the rows of the second dataframe will be appended to the rows of the first dataframe . The columns in both DataFrames should ideally match in order for the data to align correctly. If the columns do not match, pandas will automatically fill in missing values with NaN.

Let’s understand this by a simple code

Code:

import pandas as pd

df1 = pd.DataFrame({ #creating a dataframe
    'A': [1, 2],
    'B': [3, 4],
    'C': [5, 6]
})

df2 = pd.DataFrame({ #creating another dataframe 
    'A': [7, 8],
    'B': [9, 10]
})

result = pd.concat([df1, df2], axis=0)  # Concatenate along rows (axis=0)
print(result)

Output:

  A B  C
0 1 3  5.0
1 2 4  6.0
0 7 9  NaN
1 8 10 NaN

Concatenating DataFrames along Column

The below code is for concatenating two dataframes column wise

Program :

import pandas as pd

df1 = pd.DataFrame({ #creating a dataframe
    'A': [1, 2],
    'B': [3, 4]
})

df2 = pd.DataFrame({ #creating another dataframe 
    'A': [5, 6],
    'B': [7, 8]
})

result = pd.concat([df1, df2], axis=1)  # Concatenate along column (axis=1)
print(result) #printing the resultant dataframe

Output:

  A B A B
0 1 3 5 7
1 2 4 6 8

 

Here, we can understand that the two dataframes are concatenated column wise . This is by specifing axis = 1 .

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top