-6

I want to import a list of stock prices and export to excel using python. I am able to append the list of prices but not able to export. Please help. Below is the code I am new to Python.

import pandas as pd

import pandas_datareader.data as web

import datetime

import matplotlib 

import matplotlib.pyplot as plt

start = datetime.datetime(2015, 1, 1)

end = datetime.date.today()

stocklist=["ANDHRABANK.NS","HDFCBANK.NS","IDBI.NS","PNB.NS"]

prices=[]

for i in stocklist:

  f=web.DataReader(i, 'yahoo', start, end)

prices.append(f)

print prices

I get the below result.

[                 Open        High        Low      Close  Adj Close      Volume
Date                                                                          
2015-01-01  95.000000  100.949997  94.000000  96.449997  89.612679  17072046.0
2015-01-02  97.250000   98.050003  95.650002  96.250000  89.426872   4886258.0
2015-01-05  96.099998   96.800003  93.900002  94.150002  87.475739   3937461.0
2015-01-06  93.599998   93.599998  88.650002  89.349998  83.015999   4531779.0
2015-01-07  89.349998   90.500000  86.699997  88.500000  82.226265   5134680.0
2015-01-08  89.650002   91.750000  89.500000  91.000000  84.549042   3333586.0

Now I want to export and plot just the close price to an existing excel file in my Dropbox.

Tanks for the support.

River
  • 8,585
  • 14
  • 54
  • 67
Sree
  • 3
  • 4

1 Answers1

0

Try pandas.ExcelWriter

In [1]: import pandas as pd
pd
In [2]: pd.ExcelWriter?
...
Init signature: pd.ExcelWriter(cls, path, engine=None, **kwargs)
Docstring:     
Class for writing DataFrame objects into excel sheets, default is to use
xlwt for xls, openpyxl for xlsx.  See DataFrame.to_excel for typical usage.
In [6]: pd.DataFrame.to_excel?
-----
If passing an existing ExcelWriter object, then the sheet will be added
to the existing workbook.  This can be used to save different
DataFrames to one workbook:

>>> writer = pd.ExcelWriter('output.xlsx')
>>> df1.to_excel(writer,'Sheet1')
>>> df2.to_excel(writer,'Sheet2')
>>> writer.save()

you'll still need to figure out the dropbox piece

ShpielMeister
  • 1,417
  • 10
  • 23