Skip to Content
Learn
Working with Multiple DataFrames
Concatenate DataFrames

Sometimes, a dataset is broken into multiple tables. For instance, data is often split into multiple CSV files so that each download is smaller.

When we need to reconstruct a single DataFrame from multiple smaller DataFrames, we can use the method pd.concat([df1, df2, df2, ...]). This method only works if all of the columns are the same in all of the DataFrames.

For instance, suppose that we have two DataFrames:

df1
nameemail
Katja Obingerk.obinger@gmail.com
Alison HendrixalisonH@yahoo.com
Cosima Niehauscosi.niehaus@gmail.com
Rachel Duncanrachelduncan@hotmail.com
df2
nameemail
Jean Grayjgray@netscape.net
Scott Summersssummers@gmail.com
Kitty Prydekitkat@gmail.com
Charles Xaviercxavier@hotmail.com

If we want to combine these two DataFrames, we can use the following command:

pd.concat([df1, df2])

That would result in the following DataFrame:

nameemail
Katja Obingerk.obinger@gmail.com
Alison HendrixalisonH@yahoo.com
Cosima Niehauscosi.niehaus@gmail.com
Rachel Duncanrachelduncan@hotmail.com
Jean Grayjgray@netscape.net
Scott Summersssummers@gmail.com
Kitty Prydekitkat@gmail.com
Charles Xaviercxavier@hotmail.com

Instructions

1.

An ice cream parlor and a bakery have decided to merge.

The bakery’s menu is stored in the DataFrame bakery, and the ice cream parlor’s menu is stored in DataFrame ice_cream.

Create their new menu by concatenating the two DataFrames into a DataFrame called menu.

2.

Display menu using print.

Folder Icon

Sign up to start coding

Already have an account?