gpt4 book ai didi

python-3.x - 无法将我的 Pandas 数据框导出到 excel

转载 作者:行者123 更新时间:2023-12-04 20:24:36 24 4
gpt4 key购买 nike

我正在尝试将我的 pandas dataFrame(股票数据)导出到 excel。代码似乎运行正常,但是当我检查文件路径时,文档没有出现。我对此进行了进一步研究,它给出了一个错误,指出该文档不是 uft-8 编码的,但即使使用更新的代码,它仍然无法正常工作。
非常感谢任何建议。


##Importing libraries
import requests
import pandas as pd
import datetime

####Get data and convert it to a dataframe####
r = requests.get('https://finnhub.io/api/v1/stock/candle?symbol=EYEN&resolution=5&from=1597125600&to=1597161300&format=csv&token=bsq37kofkcbdt6un50lg')

df = pd.read_csv('https://finnhub.io/api/v1/stock/candle?symbol=EYEN&resolution=5&from=1597125600&to=1597161300&format=csv&token=bsq37kofkcbdt6un50lg')

df = pd.DataFrame
###Definine all Variables in dataframe###
open = df['o']
high = df['h']
low = df['l']
close = df['c']
volume = df['v']
date = df['t']

###Organise Data into columns###
df.columns['date', 'open', 'high', 'low', 'close', 'volume']

##Reorganise date from Unix to Standard Pandas##
df['date'] = pd.to_datetime(df['date'],unit='s')

##export to Excel
output_file = R'C:\Users\User\PycharmProjects\projectx\STock Data.xlsx'

df.to_excel(output_file, encoding='utf-8')

最佳答案

"df = pd.DataFrame"是一行额外的代码及其缺少的括号。
pd.read_csv() 返回一个不需要转换的 DataFrame。我猜这是导致问题的原因。
这对我有用:

##Importing libraries
import requests
import pandas as pd
import datetime

####Get data and convert it to a dataframe####
r = requests.get('https://finnhub.io/api/v1/stock/candle?symbol=EYEN&resolution=5&from=1597125600&to=1597161300&format=csv&token=bsq37kofkcbdt6un50lg')

df = pd.read_csv('https://finnhub.io/api/v1/stock/candle?symbol=EYEN&resolution=5&from=1597125600&to=1597161300&format=csv&token=bsq37kofkcbdt6un50lg')

###Definine all Variables in dataframe###
openl = df['o']
high = df['h']
low = df['l']
close = df['c']
volume = df['v']
date = df['t']

###Organise Data into columns###
df.columns = ['date', 'open', 'high', 'low', 'close', 'volume']

##Reorganise date from Unix to Standard Pandas##
df['date'] = pd.to_datetime(df['date'],unit='s')

##export to Excel
output_file = R'C:\Users\User\PycharmProjects\projectx\STock Data.xlsx''

df.to_excel(output_file, encoding='utf-8')

关于python-3.x - 无法将我的 Pandas 数据框导出到 excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63390256/

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