This tutorial shows how to use function of concat() in Python with examples. pd.concat()
can be used to concat two dataframes in Pandas.
The syntax of combining two dataframes (df_1, and df_2) by adding columns:
pd.concat ( [df_1, df_2], axis=1)
The syntax of combining two dataframes by adding rows:
pd.concat ( [df_1, df_2], axis=0)
Example 1: Concat two dataframes by adding columns
The following Python generates two sample dataframes that will be used later.
import pandas as pd
# dataframe 1: car_data
car_data = pd.DataFrame({'Brand': ['Tesla', 'Toyota','Tesla','Ford'],
'Location': ['CA', 'CA','NY','MA']},index=list('abcd'))
print("dataframe 1: car_data: \n",car_data)
# dataframe 2: car_name
car_name = pd.DataFrame({
'Name': ['Jake', 'Jacob','John','Jess','James']},index=list('abdek'))
print("dataframe 2: car_name: \n",car_name)
The following is the print out of the two dataframes.
dataframe 1: car_data: Brand Location a Tesla CA b Toyota CA c Tesla NY d Ford MA dataframe 2: car_name: Name a Jake b Jacob d John e Jess k James
The following the Python code example showing how to add columns of one dataframe to another dataframe.
# combining two dataframes by adding as new columns
df_combined=pd.concat([car_data, car_name], axis=1)
print(df_combined)
The following is the final output, which shows the combined dataframe.
Brand Location Name a Tesla CA Jake b Toyota CA Jacob c Tesla NY NaN d Ford MA John e NaN NaN Jess k NaN NaN James
Example 2: Concat two dataframes by adding rows
The following shows how to add one dataframe as new rows into another dataframe. First, we generate the sample dataframes below.
import pandas as pd
# dataframe 1: car_data_1
car_data_1 = pd.DataFrame({'Brand': ['Tesla', 'Toyota','Tesla','Ford'],
'Location': ['CA', 'CA','NY','MA']},index=list('abcd'))
print("car_data_1: \n",car_data_1)
# dataframe 2: car_data_2
car_data_2 = pd.DataFrame({'Brand': ['Honda', 'Toyota'],
'Location': ['CA', 'WA']},index=list('tj'))
print("car_data_2: \n",car_data_2)
The following is the output of two dataframes.
car_data_1: Brand Location a Tesla CA b Toyota CA c Tesla NY d Ford MA car_data_2: Brand Location t Honda CA j Toyota WA
axis
by default is 0, indicating combining dataframes as rows. Thus, actually, we do not need to add that parameter.
# combining two dataframes by adding as new rows
df_combined=pd.concat([car_data_1, car_data_2])
print(df_combined)
The following is the output of combined dataframe by adding rows in Python.
Brand Location a Tesla CA b Toyota CA c Tesla NY d Ford MA t Honda CA j Toyota WA