I have written the following code to merge a specific column in multiple csv files into a single data frame over a specified time period. Unfortunately, the following code doesn’t return the same column for all stocks, just the first one on my list. Any help here would be much appreciated.
Sample of Data frame is below:
tickers_list = [“ABBN.SW”, “ABDN.L”, “ADS.DE”, “1299.HK”, “AMC.AX”]
def merge_df_by_TI(col_name, sdate, edate, *tickers_list):
mult_df = pd.DataFrame()
for x in tickers_list:
df = get_stock_df_from_csv
mask = (df.index >= sdate) &
(df.index <= edate)
mult_df[x] = df[mask][col_name]
return df
To call the function I used the following:
mult_df = merge_df_by_TI(‘col_name’, S_DATE, E_DATE, *tickers_list)
mult_df
The get_stock_from_csv function is defined as:
def get_stock_df_from_csv(ticker):
try:
df = pd.read_csv(PATH + ticker + ’.csv’, index_col=0)
except FileNotFoundError
print(“File Does Not Exist”)
else:
return df
When I run the function I get the date column and the desired column of the first ticker in the list but not the same column for the other tickers in the list as expected.
I have checked the files for the other tickers and the columns are present for each of them. I have also checked the date range is present and the file path defined in the get_stock_from_csv function (this is basically the df.read_csv function with the filepath defined).