我问过如何迭代多个 csv 文件(例如 100 个不同的股票代码文件)并立即计算它们的每日收益。我想知道如何为每个文件调用这些返回的最大/最小值并打印报告。
以下是 Trenton McKinney 先生创建的词典:
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
问候并感谢您的帮助!
米脂
相关分类