Call a report from a dictionary of dataframes

☆樱花仙子☆ 提交于 2021-02-17 05:48:08

问题


I'm my previous question, I have asked how to iterate over multiple csv files (like 100 different files of stocks symbols) and calculate their daily returns at once. I would like to know how to call max/min values for these returns for each file and print a report.

Here is the creation of dictionaries as per Mr. 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

Regards and thanks for help!


回答1:


data_dict = dict()  # create an empty dict here
for k, df in df_dict.items():
    df_dict[k]['Return %'] = df.iloc[:, 0].pct_change(-1)*100

    # aggregate the max and min of Return
    mm = df_dict[k]['Return %'].agg(['max', 'min'])  

    # add it to the dict, with ticker as the key
    data_dict[k] = {'max': mm.max(), 'min': mm.min()}  

# convert to a dataframe if you want
mm_df = pd.DataFrame.from_dict(data_dict, orient='index')

# display(mm_df)
          max      min
aapl  8.70284 -4.90070
msft  6.60377 -4.08443

# save
mm_df.to_csv('max_min_return.csv', index=True)


来源:https://stackoverflow.com/questions/63892673/call-a-report-from-a-dictionary-of-dataframes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!