I am calculating a single stock return as follow:
data = pd.read_csv(r\'**file**.csv\')
data.index = data.Date
data[\'Return %\'] = data[\'AAPL\'].pct_change(-1)*
pathlib and .glob to create a list of all the filesdict.items().df_dict[k] addresses each dataframe, where k is the dict key, which is the file name..csv file to be read in with one Date column, not two.Date is set as the index.
: means all rows and 0 is the column index for the numeric data.df_dict.keys() will return a list of all the keysdf_dict[key].import pandas as pd
from pathlib import Path
# create the path to the files
p = Path('c:/Users/<<user_name>>/Documents/stock_files')
# get all the files
files = p.glob('*.csv')
# created the dict of dataframes
df_dict = {f.stem: pd.read_csv(f, parse_dates=['Date'], index_col='Date') for f in files}
# apply calculations to each dataframe and update the dataframe
# since the stock data is in column 0 of each dataframe, use .iloc
for k, df in df_dict.items():
df_dict[k]['Return %'] = df.iloc[:, 0].pct_change(-1)*100