Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | None | 0 0
  1. # The script MUST contain a function named azureml_main
  2. # which is the entry point for this module.
  3.  
  4. # imports up here can be used to
  5. import pandas as pd
  6.  
  7. # The entry point function can contain up to two input arguments:
  8. #   Param<dataframe1>: a pandas.DataFrame
  9. #   Param<dataframe2>: a pandas.DataFrame
  10. def azureml_main(dataframe1 = None, dataframe2 = None):
  11.  
  12.     # Execution logic goes here
  13.     print(f'Input pandas.DataFrame #1: {dataframe1}')
  14.  
  15.     # If a zip file is connected to the third input port,
  16.     # it is unzipped under "./Script Bundle". This directory is added
  17.     # to sys.path. Therefore, if your zip file contains a Python file
  18.     # mymodule.py you can import it using:
  19.     # import mymodule
  20.  
  21.     # Return value must be of a sequence of pandas.DataFrame
  22.     # E.g.
  23.     #   -  Single return value: return dataframe1,
  24.     #   -  Two return values: return dataframe1, dataframe2
  25.  
  26.          products = {}
  27.     for i in range(len(dataframe1['Продукт'])):
  28.         name = dataframe1['Продукт'][i]
  29.         if name in products:
  30.             products[name] += dataframe1['Выручка'][i]
  31.         else:
  32.             products[name] = dataframe1['Выручка'][i]
  33.     data = {}
  34.     data['Продукт'] = list(products.keys())
  35.     result = pd.DataFrame(data)
  36.     result['Выручка'] = list(products.values())
  37.  
  38.     periods = {}
  39.     for i in range(2018 * 12 + 7, 2019 * 12 + 6):
  40.         periods[i] = {}
  41.     for i in range(len(dataframe1['Продукт'])):
  42.         date = dataframe1['Дата'][i]
  43.         try:
  44.             date_azure = int(date)
  45.             base = datetime.datetime(1899,12,30)
  46.             delta = datetime.timedelta(days=date_azure)
  47.             date1 = base + delta
  48.         except ValueError:
  49.             date1 = dt.strptime(date, "%d.%m.%Y")
  50.         m = date1.month
  51.         y = date1.year
  52.         name = dateframe1['Продукт'][i]
  53.         income = dateframe1['Выручка'][i]
  54.         month = y * 12 + m
  55.         if name in periods[month]:
  56.             periods[month][name] += income
  57.         else:
  58.             periods[month][name] = income
  59.     xyz = []
  60.     for name in products:
  61.         mean = 0.0
  62.         for month in periods:
  63.             if name in periods[month]:
  64.                 mean += periods[month][name]
  65.         mean /= len(periods)
  66.         sigma = 0.0
  67.         for month in periods:
  68.             if name in periods[month]:
  69.                 sigma += (periods[month][name] - mean) ** 2
  70.             else:
  71.                 sigma += mean ** 2
  72.         sigma /= len(periods)
  73.         sigma = sigma ** 0.5
  74.  
  75.         var = 1.0 * sigma / mean
  76.         if 100 * var <= 85:
  77.             xyz.append('X')
  78.             continue
  79.         if 100 * var <= 100:
  80.             xyz.append('Y')
  81.             continue
  82.         else:
  83.             xyz.append('Z')
  84.             continue
  85.  
  86.     result['ABC'] = pd.Series(len(products), index=result.index)
  87.     for i in range(len(products)):
  88.         result['XYZ'][i] = xyz[i]
  89.     result.sort_values(by=['Выручка'], ascending=False, inplace=True)
  90.     sumOfIncomes = result['Выручка'].sum()
  91.     result = result.reset_index(drop=True)
  92.     accumulate = 0
  93.     s = result['Выручка']
  94.     for i in range(len(products)):
  95.         accumulate += s[i]
  96.         if (accumulate <= 0.7 * sumOfIncomes):
  97.             result['ABC'][i] = 'A'
  98.         elif (accumulate <= 0.9 * sumOfIncomes):
  99.             result['ABC'][i] = 'B'
  100.         else:
  101.             result['ABC'][i] = 'C'
  102.     result.drop('Выручка', axis=1, inplace=True)
  103.     return [result]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement