Combine two Series using concat():

In [1]:
import numpy as np
import pandas as pd
In [2]:
s1 = pd.Series(['p', 'q'])
In [3]:
s2 = pd.Series(['x', 'y'])
In [4]:
pd.concat([s1, s2])
Out[4]:
0    p
1    q
0    x
1    y
dtype: object

Reset the above result by setting the ignore_index option to True:

In [5]:
pd.concat([s1, s2], ignore_index=True)
Out[5]:
0    p
1    q
2    x
3    y
dtype: object

Add a hierarchical index at the outermost level of the data with the keys option:

In [6]:
pd.concat([s1, s2], keys=['s1', 's2'])
Out[6]:
s1  0    p
    1    q
s2  0    x
    1    y
dtype: object

Label the index keys with the names option:

In [7]:
pd.concat([s1, s2], keys=['s1', 's2'],
          names=['Series name', 'Row ID'])
Out[7]:
Series name  Row ID
s1           0         p
             1         q
s2           0         x
             1         y
dtype: object

Combine two DataFrame objects with identical columns:

In [8]:
df1 = pd.DataFrame([['p', 2], ['q', 3]],
                    columns=['letter', 'number'])
df1
Out[8]:
letter number
0 p 2
1 q 3
In [9]:
df2 = pd.DataFrame([['r', 4], ['s', 5]],
                   columns=['letter', 'number'])
df2
Out[9]:
letter number
0 r 4
1 s 5
In [10]:
pd.concat([df1, df2])
Out[10]:
letter number
0 p 2
1 q 3
0 r 4
1 s 5

Combine DataFrame objects with overlapping columns and return everything. Columns outside the intersection will
be filled with NaN values.

In [11]:
df3 = pd.DataFrame([['r', 4, 'lion'], ['s', 5, 'fox']],
                   columns=['letter', 'number', 'animal'])
df3
Out[11]:
letter number animal
0 r 4 lion
1 s 5 fox
In [12]:
pd.concat([df1, df3], sort=False)
Out[12]:
letter number animal
0 p 2 NaN
1 q 3 NaN
0 r 4 lion
1 s 5 fox

Combine DataFrame objects with overlapping columns and return only those that are shared by passing inner
to the join keyword argument.

In [13]:
pd.concat([df1, df3], join="inner")
Out[13]:
letter number
0 p 2
1 q 3
0 r 4
1 s 5

Combine DataFrame objects horizontally along the x axis by passing in axis=1:

In [14]:
df4 = pd.DataFrame([['hen', 'jack'], ['dog', 'jolly']],
                   columns=['animal', 'name'])
In [15]:
pd.concat([df1, df4], axis=1)
Out[15]:
letter number animal name
0 p 2 hen jack
1 q 3 dog jolly

Prevent the result from including duplicate index values with the verify_integrity option:

In [16]:
df5 = pd.DataFrame([1], index=['p'])
df5
Out[16]:
0
p 1
In [17]:
df6 = pd.DataFrame([2], index=['p'])
df6
Out[17]:
0
p 2
pd.concat([df5, df6], verify_integrity=True) Traceback (most recent call last): ... ValueError: Indexes have overlapping values: ['p']