Puzzle 17 Not My Type

Pandas Brain Teasers — by Miki Tebeka (25 / 34)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Y3K | TOC | Off with Their NaNs 👉

concat.py

​ ​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.

images/hline.png

This code will print:

​ a    float64
​ b int64
​ c float64
​ dtype: object</code></pre></td>
images/hline.png

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…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.