Reputation: 79
I am trying to export my pandas dataFrame (stock data) to excel. The code seems to run okay but when I check the file path, the document does not appear. I looked into this further and it gave an error saying the document is not uft-8 codded but even with the updated code it still doesn't work.
Any advice greatly appreciated.
##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')
Upvotes: 2
Views: 1711
Reputation: 1546
"df = pd.DataFrame" is an extra line of code and its missing brackets. pd.read_csv() returns a DataFrame no need to convert it. I would guess this is whats causing the problem.
This worked for me:
##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')
Upvotes: 1