<jupyter_start><jupyter_text>17.2 Database-style DataFrame joining/merging  17.2.7 Joining a single Index to a Multi-index<jupyter_code>import pandas as pd
left = pd.DataFrame({'A': ['A0', 'A1', 'A2'],
                     'B': ['B0', 'B1', 'B2']},
                    index=pd.Index(['K0', 'K1', 'K2'], name='key'))
left
 index = pd.MultiIndex.from_tuples([('K0', 'Y0'), ('K1', 'Y1'),('K2', 'Y2'), ('K2', 'Y3')],names=['key', 'Y'])
right = pd.DataFrame({'C': ['C0', 'C1', 'C2', 'C3'],'D': ['D0', 'D1', 'D2', 'D3']},index=index)
right
result = left.join(right, how='inner')
result
result = pd.merge(left.reset_index(), right.reset_index(),on=['key'], how='inner').set_index(['key','Y'])
result<jupyter_output><empty_output><jupyter_text>17.2.8 Joining with two multi-indexes<jupyter_code>index = pd.MultiIndex.from_tuples([('K0', 'X0'), ('K0', 'X1'),('K1', 'X2')],names=['key', 'X'])
 left = pd.DataFrame({'A': ['A0', 'A1', 'A2'],'B': ['B0', 'B1', 'B2']},index=index)
result = pd.merge(left.reset_index(), right.reset_index(),on=['key'], how='inner').set_index(['key','X','Y'])
result<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.28186714542190305, 'fraction_numerical': 0.04308797127468582, 'mean_word_length': 6.012578616352202, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '536295', 'license': 'Apache-2.0', 'n_tokens_mistral': 468, 'n_tokens_neox': 461, 'n_words': 81, 'path': 'CHAPTER 17 MERGE, JOIN, AND CONCATENATE.ipynb', 'repo_name': 'dengking/jupyter-notebook-of-pandas-official-doc', 'size': '1114'}