Advertisement
Guest User

Lab 6 - Part 2

a guest
Mar 31st, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. import pandas as pd
  2. import calendar
  3.  
  4. xlsx = pd.ExcelFile("merge.xlsx")
  5. skipRows = [1, 2]
  6. sheets = []
  7. for i, s in enumerate(xlsx.sheet_names):
  8.     sheets += [xlsx.parse(s, skiprows = skipRows[i])]
  9.  
  10. def dateToMonthYear(date):  
  11.     return calendar.month_name[int(date[5:7])] + " " + date[0:4]
  12.  
  13. for s in sheets:
  14.     s['item name'] = s['item name'].apply(str.strip)
  15.     s['date/time'] = s['date/time'].apply(dateToMonthYear)
  16.  
  17. #trans stands for transactions
  18. trans = pd.concat([s for i, s in enumerate(sheets)], ignore_index = True, sort = False)
  19. trans['purchase'] = trans["cost per unit"] * trans["units purchased"]
  20. trans['sale'] = trans["price per unit"] * trans["units sold"]
  21. trans = trans.drop(["cost per unit", "price per unit"], axis=1)
  22.  
  23. #prof stands for cumulative profits
  24. prof = pd.DataFrame(columns=['date', 'item name', "remainder", 'profit'])
  25. monthBefore = ""
  26. for month in trans["date/time"].unique():
  27.     print("\n" + month)
  28.     for itemName in sorted(trans["item name"].unique()):
  29.         itemBefore = prof[(prof['date'] == monthBefore) & (prof['item name'] == itemName)]
  30.         item  = trans[(trans['date/time'] == month) & (trans['item name'] == itemName)]
  31.         remainder = item["units purchased"].sum() - item["units sold"].sum()
  32.         remainder += int(itemBefore["remainder"]) if monthBefore else 0
  33.         profit = item["sale"].sum() - item["purchase"].sum()
  34.         profit += float(itemBefore["profit"]) if monthBefore else 0
  35.         prof.loc[-1] = [month, itemName, remainder, profit]
  36.         prof.index = prof.index + 1
  37.         print(itemName + ": remaining " + str(remainder)
  38.         + " items , profit of sales = RM " + str(profit))
  39.     monthBefore = month
  40. prof.reset_index(drop=True, inplace=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement