# automated script that grabs all performance measurements and averages them # to be run on Mondays import pandas as pd import numpy as np import matplotlib.pyplot as plt # read in the csv file and put into dataframe df = pd.read_csv('/Users/joconnor/Desktop/Me/performance_february_14_2019.csv') # convert date column to datetime object df['date'] = pd.to_datetime(df['date']) # set start date, last date, and derive number of weeks from there start_date = df['date'].iloc[0] last_date = df['date'].iloc[-1] next_date = pd.date_range(start_date, last_date, freq='W-MON') # give yourself an empty list to write all weekly calculations to columns = [] # run calculation over all data for index, date in enumerate(next_date): if index == len(next_date)-1: continue else: week = df['date'].between(date, (next_date[index+1] - pd.offsets.Day(1))) grouped_week = df[week].groupby(['action'], as_index=False).duration.mean() named_week = grouped_week.rename(columns={'duration': 'Week of ' + date.strftime('%b %d')}) columns.append(named_week) # combine all date into a pandas dataframe df2 = pd.merge(*columns, on='action', how='right') # export it to a CSV (change to sep='/t' if you want it tab deliniated) df2.to_csv('auto_performance_calculator.csv', sep=',', encoding='utf-8', index=False)