• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Pandas 101 / How To Concatenate Two or More Pandas DataFrames?

How To Concatenate Two or More Pandas DataFrames?

April 17, 2020 by cmdlinetips

Say you two dataframes of same size with same variables i.e. same columns and would like to concatenate them in to a single dataframe. Pandas’ concat() function can be handy in concatenating two or more such simple dataframes.

In this post, we will see examples of how to concatenate two dataframes into a single one. Let us load Pandas and NumPy.

# load pandas
import pandas as pd
# load numpy
import numpy as np
# check Pandas' version
pd.__version__
'1.0.0'

We will first create two data frames from scratch using numpy.

df1 = pd.DataFrame(np.random.randint(20, size=(2,3)),
                  index=list('ij'),
                   columns=list('ABC'))

Since it is a toy example, the first data frame we created has just two rows.

df1

	A	B	C
i	1	0	11
j	11	16	9

Similarly, we create a second data frame from scratch with two rows.

df2 = pd.DataFrame(np.random.randint(20, size=(2,3)),
                  index=list('mn'),
                   columns=list('ABC'))

We can verify that both the dataframes have same columns.

df2

        A	B	C
m	15	14	14
n	18	11	19

To concatenate the twp dataframes, we use concat() function with two dataframes as a list.

pd.concat([df1,df2])

        A	B	C
i	1	0	11
j	11	16	9
m	15	14	14
n	18	11	19

We can see that the two dataframes are concatenated the way we wanted.

This post is part of the series on Pandas 101, a tutorial covering tips and tricks on using Pandas for data munging and analysis.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow To Add Identifier Column When Concatenating Pandas data frames? Default ThumbnailHow to Filter Rows Based on Column Values with query function in Pandas? Default ThumbnailHow To Merge/Join DataFrames with Pandas in Python? Default ThumbnailHow To Compare Two Dataframes with Pandas compare?

Filed Under: Pandas 101

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version