尝试更新 Excel 中的日期时,只有最后一个 .to_excel 有效

问题描述 投票:0回答:1
df[df['Location'] == 'place1'].to_excel(updated_excel, sheet_name='place1', index=False)
df[df['Location'] == 'place2'].to_excel(updated_excel, sheet_name='place2', index=False)
df[df['Location'] == 'place3'].to_excel(updated_excel, sheet_name='place3', index=False)
df[df['Location'] == 'place4'].to_excel(updated_excel, sheet_name='place4', index=False)

当我打开 Excel 文件时,它只包含一张工作表 - 'place4' - 及其数据。

我已经尝试了所有方法,甚至使用了“with”,但只有最后一个有效

python-3.x pandas dataframe export-to-excel
1个回答
0
投票

如果这不是我上面评论中建议的空数据框的问题,请尝试:

with pd.ExcelWriter(updated_excel, mode = 'w') as writer:
    df[df['Location'] == 'place1'].to_excel(writer, sheet_name='place1', index=False)
    df[df['Location'] == 'place2'].to_excel(writer, sheet_name='place2', index=False)
    df[df['Location'] == 'place3'].to_excel(writer, sheet_name='place3', index=False)
    df[df['Location'] == 'place4'].to_excel(writer, sheet_name='place4', index=False)
© www.soinside.com 2019 - 2024. All rights reserved.