+3 votes
in Programming Languages by (20.2k points)
How can I concatenate two pandas dataframes along the row or column?

1 Answer

0 votes
by (35.8k points)

The concat() function of pandas can be used to concatenate pandas dataframes along a particular axis. You can check the list of arguments of this function in the link given under concat().

The parameter 'axis' is used to determine the axis to concatenate along: {0 - concatenate as new rows, 1-concatenate as new columns}. The default is 0.

Here is an example:

import pandas as pd
df1 = pd.DataFrame({"name": ['AA', 'BB', 'CC', 'DD', 'EE', 'HH', 'II'], "age": [34, 12, 56, 43, 23, 41, 52]})
df2 = pd.DataFrame({"name": ['AA', 'BB', 'CC', 'DD', 'EE', 'FF', 'GG'], "income": [3434, 1122, 2156, 4334, 54523, 4321, 6541]})
df = pd.concat([df1, df2], axis=0)
print(df)

The above code will print the following output. Since df1 and df2 don't have the same columns, it created a union of columns from both dataframes and put 'NaN' for the missing values.

 name   age   income
0   AA  34.0      NaN
1   BB  12.0      NaN
2   CC  56.0      NaN
3   DD  43.0      NaN
4   EE  23.0      NaN
5   HH  41.0      NaN
6   II  52.0      NaN
0   AA   NaN   3434.0
1   BB   NaN   1122.0
2   CC   NaN   2156.0
3   DD   NaN   4334.0
4   EE   NaN  54523.0
5   FF   NaN   4321.0
6   GG   NaN   6541.0

import pandas as pd
df1 = pd.DataFrame({"name": ['AA', 'BB', 'CC', 'DD', 'EE', 'HH', 'II'], "age": [34, 12, 56, 43, 23, 41, 52]})
df2 = pd.DataFrame({"name": ['AA', 'BB', 'CC', 'DD', 'EE', 'FF', 'GG'], "income": [3434, 1122, 2156, 4334, 54523, 4321, 6541]})
df = pd.concat([df1, df2], axis=1)
print(df)

The above code will print the following output. Since df1 and df2 have a common column, it printed duplicate columns.

 name  age name  income
0   AA   34   AA    3434
1   BB   12   BB    1122
2   CC   56   CC    2156
3   DD   43   DD    4334
4   EE   23   EE   54523
5   HH   41   FF    4321
6   II   52   GG    6541

...