首页 > 解决方案 > 熊猫合并没有给出预期的结果

问题描述

我试图合并两个数据框 df1 和 df2 并仅获取“column1”具有相同值的行并将生成的数据框写入excel。

代码:

#some other code above

if df1.empty == False and df2.empty == False:
    common_df = pd.merge(df1, df2, how='inner', on='column1')
    with pd.ExcelWriter('/usr/outfile.xlsx') as writer:
        common_df.to_excel(writer, sheet_name='common', index=False)
else:
    pass

在运行我的代码时,我在 df1 中有 20000 行,在 df2 中有 300000 行。

我的 df1 和 df2 的几行。

df1:

在此处输入图像描述df2:在此处输入图像描述

如果一切顺利,我的 common_df 中可能有大约 10000 行,并且应该可以轻松写入 excel 表。但是合并步骤正在返回一些无法写入excel的大量数据。

错误:

  File "C:\Python3\lib\site-packages\pandas\io\formats\excel.py", line 804, in write
    f"This sheet is too large! Your sheet size is: {num_rows}, {num_cols} "
ValueError: This sheet is too large! Your sheet size is: 10325130, 9 Max sheet size is: 1048576, 16384

我在这里做错什么了吗?请帮忙。提前致谢!

标签: pythonpython-3.xpandasmergepandas.excelwriter

解决方案


推荐阅读