Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How would I write columns of a series to an Excel file with this? #19

Open
ghost opened this issue Jul 3, 2017 · 3 comments
Open

How would I write columns of a series to an Excel file with this? #19

ghost opened this issue Jul 3, 2017 · 3 comments

Comments

@ghost
Copy link

ghost commented Jul 3, 2017

Basically, I am having trouble writing columns to an Excel file from a series in Fred. My issue is in the last line of the dataget function, where I have tried many things, and each give an error.

from fredapi import Fred
import pandas as pd
import time

#key = blah

API_KEY = "blah"
fred = Fred(api_key = API_KEY)

#Format = (id, description)
data_list = [('SP500', 'Stock Exchange Index'), ('UNRATE', 'Civilian Unemployment Rate')]



def dataget (id,description):
	#get a series
	values = fred.get_series(id, observation_start='1/01/2012')

	#make an Excelwriter in a workbook called "FRED_DATA"
	writer = pd.ExcelWriter('FRED_Data', engine='xlsxwriter')

	#Create a custom sheet with (`description` + data) as the worksheet name. 
	values.to_excel(writer, str(description + " Data"))
	
	#BUT I WANT THAT LINE^ TO INCLUDE COLUMNS, so something like this...
	#values.to_excel(writer, str(description + "data"),columns = (date, description))

#Break each (series_id, name) tupple into a an (id,description) pair
for tupple in data_list:
	id = tupple[0]
	description = tupple[1]
	#call dataget on this pair
	dataget(id,description)


	
	

`
@mortada
Copy link
Owner

mortada commented Jul 5, 2017

what error do you get?

@ghost
Copy link
Author

ghost commented Jul 5, 2017 via email

@Liam3851
Copy link

Liam3851 commented Jul 5, 2017

@JoshAsh I think the easiest would be a DataFrame, I think. For example, the following would put a DataFrame with your descriptions as the column names, and then write the result out to Excel:

values = pd.DataFrame({description:fred.get_series(id, observation_start='1/1/2012') for id, description in data_list})
values.to_excel('/Fred_Data.xlsx', sheet_name='FRED Data')

If you really want the data in different sheets (why?) then replace values.to_excel call in your example with
values.to_frame().rename(columns={0:description}).to_excel(...)

I suggest referring at the Pandas documentation for more about Series and DataFrame.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants