Concatenate Values of Two DataFrames
Let's find out how concatenation works in a pandas DataFrame.
We'll cover the following
Try it yourself
Try executing the code below to see the result.
import pandas as pddf1 = pd.DataFrame([[1, 2], [3, 4]], columns=['a', 'b'])df2 = pd.DataFrame([[5, 6], [7, 8]], columns=['b', 'c'])df = pd.concat([df1, df2])print(df.dtypes)
Explanation
If we look at the dtypes of df1
and df2
, we’ll see they’re int64
:
In [1]: df1.dtypes
Out[1]:
a int64
b int64
dtype: object
In [2]: df2.dtypes
Out[2]:
b int64
c int64
dtype: object
Why did the teaser output show the a
and c
columns as float64
?
The pandas.concat
can handle frames with different columns. By default, it will assume nan
values in the missing labels for a specific column. As we saw in
the puzzle Div Sum, pandas will change the dtype of a series to allow missing values, and that’s what’s happening here.
Get hands-on with 1300+ tech skills courses.