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”,但只有最后一个有效
如果这不是我上面评论中建议的空数据框的问题,请尝试:
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)