How to merge / concatenate two DataFrames with pandas in python ?

How to merge / concatenate two DataFrames with pandas in python ?

Daidalos April 14, 2020


Basic examples of how to merge / concatenate two DataFrames with pandas in python:

Create two data frames

Let's create a first data frame called df1 with pandas

>>> import pandas as pd
>>> import numpy as np
>>> data = np.arange(1,13)
>>> data = data.reshape(3,4)
>>> df1 = pd.DataFrame(data=data,columns=['a','b','c','d'])
>>> df1
   a   b   c   d
0  1   2   3   4
1  5   6   7   8
2  9  10  11  12

Create a second data frame df2

>>> df2 = pd.DataFrame(data=[[13,14,15,16],[17,18,19,20]],columns=['a','b','c','d'])
>>> df2
    a   b   c   d
0  13  14  15  16
1  17  18  19  20

Concatenate on axis 0 (rows)

To append df2 to df1, a solution is to use the pandas function concat():

>>> df3 = pd.concat([df1,df2], ignore_index=True)
>>> df3
    a   b   c   d
0   1   2   3   4
1   5   6   7   8
2   9  10  11  12
3  13  14  15  16
4  17  18  19  20

Concatenate on axis 1 (columns)

Create a new data frame d2:

>>> df2 = pd.DataFrame(data=[[13,14],[15,16],[17,18]],columns=['e','f'])
>>> df2
    e   f
0  13  14
1  15  16
2  17  18

that can be added to df1 using the function concat() with option axis=1:

>>> df3 = pd.concat([df1,df2], axis=1)
>>> df3
   a   b   c   d   e   f
0  1   2   3   4  13  14
1  5   6   7   8  15  16
2  9  10  11  12  17  18

Go further

For more advance features see Merge, join, and concatenate sur pandas.pydata.org.

References

Links Site
Merge, join, and concatenate pandas doc
concat() pandas.pydata.org
Pandas : How to create an empty DataFrame and append rows & columns to it in python thispointer.com
Add one row to pandas DataFrame stackoverflow
Adding new column to existing DataFrame in Pandas stackoverflow