I have an Excel file (Compay, Start_Date, End_Date) from which I want to read the components and find the stock data available from the NSE website and save that data to a CSV file.
So I am trying this code:
cf = pd.read_csv('Company.csv')
cf['START_DT']=pd.to_datetime(cf['START_DT'])
cf['END_DT']=pd.to_datetime(cf['END_DT'])
cf
OUTPUT -
COMPANY START_DT END_DT
0 SBIN 2014-01-01 2018-01-01
1 PNB 2014-01-01 2018-01-01
2 INFY 2014-01-01 2018-01-01
for index,row in cf.iterrows():
start_dt=row['START_DT']
end_dt=row['END_DT']
data = get_history(symbol=row['COMPANY'], start=start_dt, end=end_dt)
print(data)
data.to_csv('data.csv', sep=',')
Howevery, the data stored is only the data of the last company. I have tried to append the data using a dataframe but it does not work neither.
How can I fix this?
Here is necessary create list of all DataFrames via append and concat together:
dfs = []
for index,row in cf.iterrows():
start_dt=row['START_DT']
end_dt=row['END_DT']
data = get_history(symbol=row['COMPANY'], start=start_dt, end=end_dt)
dfs.append(data)
df = pd.concat(dfs)
df.to_csv('data.csv')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With