这个错误是由于左边的 DataFrame 'df' 的索引只有一级,而右边的 DataFrame 'df_threshold1' 的索引有两级导致的。解决这个问题的方法是将右边的 DataFrame 的多级索引转换为单级索引。

可以使用 'reset_index()' 方法将右边的 DataFrame 的多级索引转换为单级索引,然后再进行合并操作。

以下是修改后的代码:

df_threshold1 = df_threshold1.reset_index()
df = pd.merge(df, df_threshold1, how='left', on=['归属大区','归属城市'])

这样就可以成功合并两个 DataFrame 了。

Pandas 合并 DataFrame 报错:Not allowed to merge between different levels

原文地址: https://www.cveoy.top/t/topic/dTR2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录