user13277982
user13277982

Reputation:

Python save stock data to excel file

I created that code that reads stock data with pandareader from yahoo. I want to write the output, 7 columns automatically into an Excel file so I wrote this:

from pandas_datareader import data

start_date = '2010-01-01'
end_date = '2016-12-31'


panel_data = data.DataReader('INPX', 'yahoo', start_date, end_date)
panel_data.to_excel('data.xlsx')

But i get:

NameError: name 'df' is not defined

Upvotes: 1

Views: 343

Answers (1)

Anup Tiwari
Anup Tiwari

Reputation: 494

You need to convert the panel_data to a pandas dataframe before exporting to excel

import pandas as pd
from pandas_datareader import data

start_date = '2010-01-01'
end_date = '2016-12-31'

panel_data = data.DataReader('INPX', 'yahoo', start_date, end_date)
pd.DataFrame(panel_data).to_excel("data.xlsx") #Export to excel

This generates the excel workbook data.xlsx in the same folder as script.

Upvotes: 1

Related Questions