I am getting data every minute through an API call. Now I want to add this data to a CSV file. The data should update to the CSV after it gets new data.
I am using this code:
num_points = 1
df_setup = ig_service.fetch_historical_prices_by_epic_and_num_points(epic, resolution, num_points)
df_setup_prices_ask = df_setup['prices']['ask']
panda_df = pd.DataFrame(df_setup_prices_ask)
time.sleep(60)
while True:
stream_close_price = ig_service.fetch_historical_prices_by_epic_and_num_points(epic, resolution, num_points)
df_last_close = stream_close_price['prices']['ask']
df_test = pd.DataFrame(df_last_close)
combined_data = pd.concat([panda_df,df_test], axis=1)
combined_data.to_csv('data.csv')
print(df_last_close)
time.sleep(60)
However I can't figure out how to get this working. If I use the code above I get following output (only the "newest" data gets saved to the CSV):
DateTime,Open,High,Low,Close,Open,High,Low,Close
2022-09-21 14:34:00,143.992,143.995,143.99,143.992,,,,
2022-09-21 14:36:00,,,,,143.977,143.978,143.975,143.978
Now when I use .join using this code:
combined_data = panda_df.join(df_test)
which give this error:
ValueError: columns overlap but no suffix specified: Index(['Open', 'High', 'Low', 'Close'], dtype='object')
Now I of course tried combined_data = panda_df.join(df_test, on = 'DateTime')
However this throws the key error for DateTime
Next up I tried using merge with this code which does not work because it only returns an empty CSV:
DateTime,Open_x,High_x,Low_x,Close_x,Open_y,High_y,Low_y,Close_y
Also I know that there is probably an easier way than doing a call to create a df to use as a base to join but I haven't figured that out.