Skip to Content
Learn Python by Building Data Science Applications
book

Learn Python by Building Data Science Applications

by Philipp Kats, David Katz
August 2019
Beginner
482 pages
12h 56m
English
Packt Publishing
Content preview from Learn Python by Building Data Science Applications

Merging

On occasion, we need to join multiple dataframes together. There could be different ways to do that—let's take a look.

First and foremost, if you have multiple dataframes with the same columns, and you want to join them—never do that iteratively—try to do that once, by passing a list of all of them to the pd.concat function with the axis=0 and sort=False arguments (unless you need to sort them):

>>> df.shape(3, 4)>>> double = pd.concat([df, df], axis=0, sort=False)>>> double.shape(6, 4)

Similarly, pd.concat can merge multiple dataframes horizontally if axis=1:

>>> pd.concat([df, df], axis=1)  x y z     new_column x y z     new_column0 1 a False -1         1 a False -11 2 b True  -1         2 b True  -12 3 c False -1         3 c False -1

In this example, we group two ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python for Data Science

Python for Data Science

Yuli Vasiliev
Introduction to Machine Learning with Python

Introduction to Machine Learning with Python

Andreas C. Müller, Sarah Guido

Publisher Resources

ISBN: 9781789535365Supplemental Content