gpt4 book ai didi

python - 通过 Python 将股票数据下载到 excel(缺少日期列)

转载 作者:行者123 更新时间:2023-12-04 22:17:39 25 4
gpt4 key购买 nike

我想将股票数据下载到单个 excel 文件中,但是在执行此操作时,我遇到了一个问题,即我很难为我的股票数据库获取日期列。
Goal - Picture example
有人可以帮我找出为什么我会得到这样的结果吗?我该如何解决这个问题?

import pandas as pd
import pandas_datareader as pdr
import datetime as dt

file = str(input('Enter File Name - '))

download_sourrce = (r"C:\Users\vladt\OneDrive\Рабочий стол\Stock\{}.xlsx".format(file))
writer = pd.ExcelWriter(download_sourrce, engine='xlsxwriter')

should_continue = True

while should_continue:

x = str(input('Stock? - '))

start = dt.datetime(2020,1,1)
end = dt.datetime.today()

df = pdr.get_data_yahoo(x,start,end)

df.to_excel(writer, sheet_name=x, index=False)

should_continue = input('Add new stock? [Y/N]').upper() == 'Y'

writer.save()

最佳答案

在保存到 Excel 之前尝试以下操作:

df = df.reset_index()
通常有来自 pandas datareader(来自 yahoo 或其他为 pandas datareader 提供数据读取器的公司)或其他股票经纪人提供的其他 API/包的股票数据馈送,以将日期列设置为行索引。
对于这种设置为行索引的日期系列,您必须重置索引才能将其内容放入数据列。

关于python - 通过 Python 将股票数据下载到 excel(缺少日期列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67474494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com