如何在不删除前一个循环数据的情况下使用 Pandas 创建 ForLoop?



我使用字典创建了下面的for循环:

for position, url in dict.items():
dfs = pd.read_html(url)
df = dfs[0]
df2 = df[['Summary', 'Next dividend']]
print(position)
print(df2)
df2.to_excel(today_source_file, sheet_name=position)

我面临的问题是,每个循环都只是擦除以前的数据并重新开始,所以我只剩下"最终"循环中的数据。

我的dict中总共有14个项目,所以我希望用14个单独的选项卡来结束脚本。

ExcelWriter:封装代码

with pd.ExcelWriter(today_source_file) as writer:
# your loop here
df2.to_excel(writer, sheet_name=position)

最新更新