【发布时间】:2018-06-09 17:50:24
【问题描述】:
我正在尝试与熊猫执行合并。这两个文件有一个公用密钥(“KEY_PLA”),我试图将其与左连接一起使用。但不幸的是,从第二个文件传输到第一个文件的所有列都有 NaN 值。
这是我到目前为止所做的:
df_1 = pd.read_excel(path1, skiprows=1)
df_2 = pd.read_excel(path2, skiprows=1)
df_1.columns = ["Index", "KEY", "KEY_PLA", "INFO1", "INFO2"]
df_2.columns = ["Index", "KEY_PLA", "INFO4"]
df_1.drop(["Index"], axis=1, inplace=True)
df_2.drop(["Index"], axis=1, inplace=True)
# Merge all dataframes
df_merge = pd.DataFrame()
df_merge = df_1.merge(df_2, left_on="KEY_PLA", right_on="KEY_PLA", how="left")
print(df_merge)
这里是excel文件:
代码有什么问题?我还检查了类型,甚至将列转换为字符串。但没有任何效果。
【问题讨论】:
-
请发帖
df_1.head().to_dict('list')和df_2.head().to_dict('list')这样我们就不必下载 MB 来查看这个问题了。
标签: python pandas dataframe merge