I have the following code:
import pandas as pd
import datetime
import pandas as pd
from pandas_datareader import data as web
import matplotlib.pyplot as plt
from alpha_vantage.foreignexchange import ForeignExchange
import os
from os import path
from alpha_vantage.timeseries import TimeSeries
import matplotlib.pyplot as plt
import sys
while True:
if path.exists('stockdata.csv') == True:
data1 = pd.read_csv('stockdata.csv')
ts = TimeSeries(key='1ORS1XLM1YK1GK9Y', output_format='pandas')
data, meta_data = ts.get_intraday(symbol = 'spy', interval='1min', outputsize='full')
data = data.rename(columns={'1. open':'Open','2. high': 'High','3. low': 'Low', '4. close':'Close', '5. volume': 'Volume'})
data1 = data1.append(data)
data1.to_csv('stockdata.csv', sep= ' ')
break
else:
data1 = pd.DataFrame(columns=['Open','High','Low', 'Close','Volume'])
data1.to_csv('stockdata.csv', sep= ' ')
What i am trying to do is to check if file stockdata.csv
is in in the current directory. If it is not found then create the file.
If the file is found then download spy ticker data in data
and append that data to data1
and save it in csv file.
The output of data1 looks like this:
Problems
- How do i get rid of
Unnamed:0
column and why is it there? - How can i check and remove dublicate data in
data
and append that todata1
?