【发布时间】:2021-09-26 00:22:03
【问题描述】:
我在 .csv 文件中列出了 5 个股票代码。我正在使用下面的循环从每个符号中获取选项数据。理想情况下,所有 5 个符号的输出都将保存在 .xlsx 文件中
如果我执行 print(df_puts),我会看到数据框中的所有符号。但是,输出 .xlsx 文件仅包含 .csv 文件中最后一个符号的数据。基本上它从最后一个循环符号打印数据,而不是从循环内的所有符号打印数据
我对 pandas 和 python 很陌生。我想了解为什么未来的项目会发生这种情况
stocklist = pd.read_excel(filePath)
for i in stocklist.index:
stock=str(stocklist["Symbols"][i])
#df = pdr.get_data_yahoo(stock, start, now, threads=False)
option_dict = options.get_options_chain(stock)
#print(option_dict)
df_puts = pd.DataFrame.from_dict(option_dict.get("puts"))
df_calls = pd.DataFrame.from_dict(option_dict.get("calls"))
newFile = os.path.dirname(filePath1) + "/OptionsOutput.xlsx"
writer = ExcelWriter(newFile)
df_puts.to_excel(writer, "puts", float_format="%.3f")
df_calls.to_excel(writer, "calls", float_format="%.3f")
writer.save()
【问题讨论】:
-
你可以上传使用的文件吗?
-
@Anwarvic 您的意思是带有符号的 .csv 文件?输出 .xlsx 还是整个 python 文件?
-
我的意思是 CSV 文件,抱歉
-
我对@Tanishq 提供的答案很满意,但还是谢谢你!