Merging on index

Sometimes the keys for merging dataframes are located in the dataframes index. In such a situation, we can pass left_index=True or right_index=True to indicate that the index should be accepted as the merge key.

Merging on index is done in the following steps:

  1. Consider the following two dataframes:
left1 = pd.DataFrame({'key': ['apple','ball','apple', 'apple', 'ball', 'cat'], 'value': range(6)})right1 = pd.DataFrame({'group_val': [33.4, 5]}, index=['apple', 'ball'])

If you print these two dataframes, the output looks like the following screenshot:

Note that the keys in the first dataframe are apple, ball, and cat. In the ...

Get Hands-On Exploratory Data Analysis with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.