Advertisement
khaiwen1111

apple

Mar 23rd, 2020
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. import pandas as pd
  2. import calendar
  3.  
  4. xlsx = pd.ExcelFile(r"D:\Users\abc\abc\merge.xlsx")
  5. sr = [1, 2]
  6. sheets = []
  7.  
  8. for i, j in enumerate(xlsx.sheet_names):
  9.     sheets += [xlsx.parse(j, skiprows=sr[i])]
  10.    
  11. for s in sheets:
  12.     s['item name'] = s['item name'].apply(str.strip)
  13.  
  14. df_apple = [item[item['item name'] == 'apple'] for item in sheets]
  15.  
  16. df = pd.concat([item for item in df_apple], ignore_index = True, sort = False)
  17. df=df.fillna(0)
  18.  
  19. #split time into month years
  20. df=df.rename(columns={"date/time": "date"})
  21. def change_to_date(string):
  22.     seq = (string[:5],string[5:7])
  23.     return ''.join(seq)
  24.  
  25. df['DATE'] = df['date'].apply(change_to_date)
  26. df=df.drop(columns=["date"])
  27. cols = df.columns.tolist()
  28. cols = cols[-1:] + cols[:-1]
  29. df = df[cols]
  30. df['DATE'] = pd.to_datetime(df['DATE'])
  31. df['year'] = df['DATE'].dt.year
  32. df["months"]= df['DATE'].dt.month
  33. df=df.drop(columns=["DATE"])
  34. cols = df.columns.tolist()
  35. cols = cols[-2:] + cols[:-2]
  36. df = df[cols]
  37.  
  38. #replace all NAN into 0
  39. df['months'] = df['months'].replace(np.nan, 0)
  40. df['units purchased'] = df['units purchased'].replace(np.nan, 0)
  41. df['units sold'] = df['units sold'].replace(np.nan, 0)
  42. df['cost per unit'] = df['cost per unit'].replace(np.nan, 0)
  43. df['price per unit'] = df['price per unit'].replace(np.nan, 0)
  44.  
  45. #simple normal way
  46. #df["remainder"]=df["units purchased"]-df["units sold"]
  47. #df["profit"]=df["units sold"]*df["price per unit"]-df["units purchased"]*df["cost per unit"]
  48.  
  49. #list comprehension, faster computation speed
  50. df["remainder"]=[x - y  for x, y in zip(df["units purchased"],df["units sold"])]
  51. df["profit"]=[x* y -a*b  for x, y,a,b  in zip(df["units sold"],df["price per unit"],df["units purchased"],df["cost per unit"])]
  52.  
  53. #change months into int for conversion
  54. df['months'] = df['months'].round(0).astype(int)
  55. new_df=df.groupby(["year","months","item name"]).sum()
  56. new_df=new_df.reset_index()
  57.  
  58. #convert numeric month to worded form
  59. new_df['months'] = new_df['months'].apply(lambda x: calendar.month_abbr[x])
  60.  
  61. #convert years to int form
  62. new_df['year'] = new_df['year'].round(0).astype(int)
  63.  
  64. #loop
  65. for index, row in new_df.iterrows():
  66.     print(row["months"],row["year"])
  67.     print("\t",row["item name"],":","remaining",row["remainder"],"items,","profit of sales = $",row["profit"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement