KaleChips
KaleChips

Reputation: 41

how to save Python pandas data into excel file?

I am trying to load data from the web source and save it as a Excel file but not sure how to do it. What should I do? The original dataframe has different columns. Let's say that I am trying to save 'Open' column

import matplotlib.pyplot as plt
import pandas_datareader.data as web
import datetime
import pandas as pd

def ViewStockTrend(compcode):        
    start = datetime.datetime(2015,2,2)
    end = datetime.datetime(2016,7,13)
    stock = web.DataReader(compcode,'yahoo',start,end)    
    print(stock['Open'])


compcode = ['FDX','GOOGL','FB'] 
aa= ViewStockTrend(compcode)

Upvotes: 2

Views: 6182

Answers (2)

Alex
Alex

Reputation: 12913

Once you have made the pandas dataframe just use to_excel on the entire thing if you want:

aa.to_excel('output/filename.xlsx')

Upvotes: 3

Max Uppenkamp
Max Uppenkamp

Reputation: 974

If stock is a pandas DataFrame, you need to construct a new Framefrom that column and output that one to excel:

df = pd.DataFrame(stock['Open'])
df.to_excel('path/to/your/file')

Upvotes: 2

Related Questions