我正在尝试将我的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')
答案 0 :(得分:1)
“ 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')