Sometimes, we wanna couple multiple dataframes together. In this note, I use
df as DataFrame, s as Series.There are 4 types of merging, like in SQL.
- Inner: only includes elements that appear in both dataframes with a common key.
- Outer: includes all data from both dataframes.
- Left: includes all of the rows from the "left" dataframe along with any rows from the "right" dataframe with a common key; the result retains all columns from both of the original dataframes.
- Right: includes all of the rows from the "right" dataframe along with any rows from the "left" dataframe with a common key; the result retains all columns from both of the original dataframes.
On the same column name
On the different column names
The result keeps both
Col_1 and Col_X while in the case of the same column name, there is only 1 column. Other words, in this case, we only want to keep Col_1 and don't need Col_X. How to do that?We consider a situation in that we need to combine 2 dfs containing missing values in each. The missing values will be filled by taking from the others. For example, the value of
C in the left df can be fulfilled by the value of in the right df.