Concatenating data

Concatenation in pandas is the process of either adding rows to the end of an existing Series or DataFrame object or adding additional columns to a DataFrame. In pandas, concatenation is performed via the pandas function pd.concat(). The function will perform the operation on a specific axis and as we will see, will also perform any required set logic involved in aligning along that axis.

The general syntax to concatenate data is to pass a list of objects to pd.concat(). The following performs a concatenation of two Series objects:

In [2]:
   # two Series objects to concatenate
   s1 = pd.Series(np.arange(0, 3))
   s2 = pd.Series(np.arange(5, 8))
   s1

Out[2]:
   0    0
   1    1
   2    2
   dtype: int64

In [3]:
   s2

Out[3]:
   0    5
   1    6
   2    7
 dtype: int64 ...

Get Learning pandas now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.