Merging DataFrames with pd.merge
Another common task in reshaping data is referred to as merging, or in some cases, joining, with the latter term being used frequently in database terminology. Where concatenation “stacks” objects on top of or next to one another, a merge works by finding a common key (or set of keys) between two entities and using that to blend other columns from the entities together:
Figure 7.3: Merging two pd.DataFrame objects
The most commonly used method in pandas to perform merges is pd.merge
, whose functionality will be covered throughout this recipe. Another viable, though less commonly used, pd.DataFrame.join
method can be used as well, although knowing pd.merge
first is helpful before discussing that (we will cover pd.DataFrame.join
in the next recipe).
How to do it
Let’s continue along with the stock pd.DataFrame
objects we created in the Concatenating pd.DataFrame objects recipe:
df_q1 = pd.DataFrame([
...