Book Image

Learning Pandas

By : Michael Heydt
Book Image

Learning Pandas

By: Michael Heydt

Overview of this book

Table of Contents (19 chapters)
Learning pandas
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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

In [4]:
   # concatenate them
   pd.concat([s1, s2])

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

Two DataFrame objects can also be similarly concatenated.

...