Skip to content

Instantly share code, notes, and snippets.

@danromero
Last active January 13, 2017 19:57
Show Gist options
  • Select an option

  • Save danromero/ca071422de07559ff4cc61cf6307887c to your computer and use it in GitHub Desktop.

Select an option

Save danromero/ca071422de07559ff4cc61cf6307887c to your computer and use it in GitHub Desktop.
Python script to estimate monthly revenue based on daily totals so far in a given month
import pandas as pd
import numpy as np
import time
from datetime import datetime
from calendar import monthrange
import locale
import os
locale.setlocale(locale.LC_ALL, 'en_US')
this_year = int(time.strftime("%y"))
this_month = int(time.strftime("%m"))
days_in_this_month = max(monthrange(this_year, this_month))
files = os.listdir("/Users/danromero/Desktop")
files = [ fi for fi in files if fi.endswith(".csv") ]
prompt = "Choose a file: "
def menu(list, question):
for entry in list:
print(list.index(entry), " ", entry)
return input(prompt)
file_to_read = menu(files, prompt)
df = pd.read_csv(files[int(file_to_read)], parse_dates=True)
df = df[[0,3]]
df.columns = ['date', 'revenue']
df['month'] = df['date'].map(lambda x: datetime.strptime(x,'%Y-%m-%d').month)
df1 = df[(df['month'] == this_month) & (df['revenue'] > 0)]
total_revenue_to_date = df1['revenue'].sum()
total_days_to_date = df1['revenue'].count()
run_rate = total_revenue_to_date / total_days_to_date * days_in_this_month
print ("Total revenue this month so far:", locale.format("%d", total_revenue_to_date, grouping=True))
print ("Total days in data set:", total_days_to_date)
print ("Estimated revenue for this month:", locale.format("%d", run_rate, grouping=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment