Puzzle 22 Find Me a Phone Booth

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 What’s the Points? | TOC | Chain of Commands 👉

identities.py

​ ​import​ ​pandas​ ​as​ ​pd​

​ df1 = pd.DataFrame({
​ ​'id'​: [1, 2, 3],
​ ​'name'​: [​'Clark Kent'​, ​'Diana Prince'​, ​'Bruce Wayne'​],
​ })

​ df2 = pd.DataFrame({
​ ​'id'​: [2, 1, 4],
​ ​'hero'​: [​'Wonder Woman'​, ​'Superman'​, ​'Aquaman'​],
​ })

​ df = pd.merge(df1, df2, on=​'id'​)
​ ​print​(df)

Guess the Output

IMPORTANT

Try to guess what the output is before moving to the next page.

images/hline.png

This code will print:

​     id          name          hero
​ 0 1 Clark Kent Superman
​ 1 2 Diana Prince Wonder Woman</code></pre></td>
images/hline.png

Pandas merge[1] gets a sequence of pandas.DataFrame to merge and an optional column to merge on. If the column is not provided, Pandas will use the index of each DataFrame for merging.

The question is, what happens when one merge column has values that the other doesn’t? This…

--

--

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.