Puzzle 22 Find Me a Phone Booth
Pandas Brain Teasers — by Miki Tebeka (30 / 34)
Published in
3 min readMar 23, 2022
👈 What’s the Points? | TOC | Chain of Commands 👉
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.
This code will print:
id name hero
0 1 Clark Kent Superman
1 2 Diana Prince Wonder Woman</code></pre></td>
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…