Pandas borrows a core concept from SQL:
the Join
But there are so many different types of joining two DataFrames
Let's make this easy and go through Left, Right, Inner and Outer joins
Knowing these is great in interviews in addition to your usual DataFrame shenanigans
Joins always happen on two DataFrames.
This explanation will use: π©πͺβ¬
- π©: The left DataFrame
- πͺ: The right DataFrame
- β¬: The result
Don't get the word "join" wrong though, you can actually end up with a smaller DataFrame β¬ than either or π©πͺ
β¬ οΈ The Left Join is selfish
This one takes the complete left DataFrame π© and only checks for overlaps from the right πͺ
No πͺ from outside of the bounds of π© will make it into β¬
β‘οΈ The Right Join is almost the same as Left
Only take everything in πͺ and overlapping π©
β€΅οΈ The Inner Join
This one is tricky.
Almost always β¬ will be smaller than π©&πͺ.
For the Inner join, you only look at the parts of π© and πͺ that overlap.
Nothing is included in β¬ that exists outside of this common area.
βοΈ The Outer Join
Is possibly the simples one.
It is exactly what we would expect from a "join".
Take all of π© and all of πͺ and combine it into β¬.
All of the data is in our result.
TL;DR
- Pandas borrows from SQL using Joins
- Left and Right join maintain the original and whatever overlaps in the other
- Inner is only the common ground
- Outer uses all the data in both DataFrames
Top comments (0)