Puzzle 17 Not My Type
Pandas Brain Teasers — by Miki Tebeka (25 / 34)
Published in
2 min readMar 23, 2022
👈 Y3K | TOC | Off with Their NaNs 👉
import pandas as pd
df1 = 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)
Guess the Output
IMPORTANT
Try to guess what the output is before moving to the next page.
This code will print:
a float64
b int64
c float64
dtype: object</code></pre></td>
If you look at the dtypes of df1 and df2, you’ll see they are 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?
pandas.concat can handle frames with different columns. By default it will assume there are nan values in the missing labels for a specific column. As…