{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np \n",
    "import glob\n",
    "import os \n",
    "from openpyxl import load_workbook\n",
    "from openpyxl.styles import PatternFill, Font\n",
    "from datetime import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "hoje = datetime.today().strftime('%Y-%m-%d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#precisa ver duplicidade de desc e vendas irece."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_tabela = pd.read_excel(r\"C:\\Users\\joao.herculano\\GRUPO GINSENG\\Assistência Suprimentos - 2025\\SUPRIMENTOS\\DB_PROMOÇÕES\\BOTICARIO\\C15\\TABELA DE PEDIDOS\\Pedidos Semanais Especiais - BOT - 202515 (1).xlsx\")\n",
    "\n",
    "df_tabela = df_tabela[df_tabela['Ação revendedor'].notna() | df_tabela['Ação consumidor'].notna()]\n",
    "\n",
    "df_tabela = df_tabela[df_tabela['Região'] == 'NNE'] \n",
    "\n",
    "df_tabela = df_tabela[(df_tabela['Canal'] != 'Ecomm')]\n",
    "\n",
    "#df_tabela['Canal'] = np.where((df_tabela['Canal'] == \"Loja\") | (df_tabela['Canal'] == \"Todos\") | (df_tabela['Canal'] == \"Loja | VD\"),\"TODOS\",\"VD\")\n",
    "\n",
    "df_tabela = df_tabela[(df_tabela['Categoria'] != \"EMBALAGENS\") | (df_tabela['Categoria'] != \"SUPORTE À VENDA\")]\n",
    "\n",
    "#df_tabela = df_tabela[df_tabela['Tipo de pedido'] == 'Semanal']\n",
    "\n",
    "df_tabela = df_tabela[~df_tabela['Descrição'].str.contains('PRM')]\n",
    "\n",
    "df_tabela = df_tabela[df_tabela['Tipo de produto']!= 'EDICAO LIMITADA']\n",
    "\n",
    "df_tabela['Ação revendedor'] = np.where(df_tabela['Ação revendedor'].isna(),df_tabela['Ação consumidor'],df_tabela['Ação revendedor'])\n",
    "\n",
    "df_tabela['Percentual de desconto revendedor'] = np.where(df_tabela['Percentual de desconto revendedor'].isna(),df_tabela['Percentual de desconto consumidor'],df_tabela['Percentual de desconto revendedor'])\n",
    "\n",
    "df_tabela['MATCH'] = 1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\joao.herculano\\AppData\\Local\\Temp\\ipykernel_2580\\2225685327.py:39: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.\n",
      "  dfi = pd.read_sql(query, conn)\n"
     ]
    }
   ],
   "source": [
    "import pyodbc\n",
    "import configparser\n",
    "\n",
    "#query de vendas por ciclo\n",
    "\n",
    "config = configparser.ConfigParser()\n",
    "config.read(r\"C:\\Users\\joao.herculano\\Documents\\Enviador de email\\credenciais.ini\")\n",
    "\n",
    "conn = pyodbc.connect(\n",
    "    f\"DRIVER={{SQL Server}};\"\n",
    "    f\"SERVER={config['banco']['host']},1433;\"\n",
    "    f\"DATABASE=GINSENG;\"\n",
    "    f\"UID={config['banco']['user']};\"\n",
    "    f\"PWD={config['banco']['password']}\"\n",
    ")\n",
    "\n",
    "query = f'''\n",
    "SELECT \n",
    "    B.PDV, \n",
    "    B.SKU,\n",
    "    bd.SKU2, \n",
    "    COALESCE(bd.SKU2, b.SKU) AS SKU_FINAL,\n",
    "    B.DESCRICAO,\n",
    "    SUM(CAST(b.VENDAS AS DECIMAL(18,2))) AS VENDAS_CICLO,\n",
    "    C.Ciclo\n",
    "FROM base_vendas_bi b\n",
    "INNER JOIN ciclos_data_2025 c \n",
    "    ON CAST(b.[DATA] AS DATE) = CONVERT(DATE, c.[Date], 103) AND C.MARCA = 'BOT'\n",
    "LEFT JOIN base_depara bd on b.SKU = bd.SKU \n",
    "WHERE CAST(b.[DATA] AS DATE) >= DATEADD(YEAR, -1, GETDATE())\n",
    "GROUP BY\n",
    "    B.PDV, \n",
    "    B.SKU,\n",
    "    bd.SKU2,\n",
    "    B.DESCRICAO,\n",
    "    C.Ciclo\n",
    "HAVING SUM(CAST(b.VENDAS AS DECIMAL(18,2))) > 0\n",
    "'''\n",
    "dfi = pd.read_sql(query, conn)\n",
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfi['SKU2'] = dfi['SKU2'].fillna(\"-\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PDV</th>\n",
       "      <th>SKU</th>\n",
       "      <th>SKU2</th>\n",
       "      <th>SKU_FINAL</th>\n",
       "      <th>DESCRICAO</th>\n",
       "      <th>VENDAS_CICLO</th>\n",
       "      <th>Ciclo</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>20998</td>\n",
       "      <td>79437</td>\n",
       "      <td>-</td>\n",
       "      <td>79437</td>\n",
       "      <td>QDB GLIT SLT P/OLHO ROSELITTER 1g RPCK</td>\n",
       "      <td>1.0</td>\n",
       "      <td>C202413</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4560</td>\n",
       "      <td>51226</td>\n",
       "      <td>-</td>\n",
       "      <td>51226</td>\n",
       "      <td>CBEM SAB BARRA NUVEM V5 2x80g</td>\n",
       "      <td>30.0</td>\n",
       "      <td>C202413</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>21277</td>\n",
       "      <td>48290</td>\n",
       "      <td>-</td>\n",
       "      <td>48290</td>\n",
       "      <td>NSPA DES COL BDY SPLSH AMEI 200ml V2</td>\n",
       "      <td>3.0</td>\n",
       "      <td>C202414</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>23701</td>\n",
       "      <td>56810</td>\n",
       "      <td>-</td>\n",
       "      <td>56810</td>\n",
       "      <td>MATCH COND SCIENCE CRESCIMENTO V2 280ml</td>\n",
       "      <td>2.0</td>\n",
       "      <td>C202505</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5699</td>\n",
       "      <td>56027</td>\n",
       "      <td>-</td>\n",
       "      <td>56027</td>\n",
       "      <td>MEN SAB BARRA 2x90g</td>\n",
       "      <td>37.0</td>\n",
       "      <td>C202506</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     PDV    SKU SKU2  SKU_FINAL                                DESCRICAO  \\\n",
       "0  20998  79437    -      79437   QDB GLIT SLT P/OLHO ROSELITTER 1g RPCK   \n",
       "1   4560  51226    -      51226            CBEM SAB BARRA NUVEM V5 2x80g   \n",
       "2  21277  48290    -      48290     NSPA DES COL BDY SPLSH AMEI 200ml V2   \n",
       "3  23701  56810    -      56810  MATCH COND SCIENCE CRESCIMENTO V2 280ml   \n",
       "4   5699  56027    -      56027                      MEN SAB BARRA 2x90g   \n",
       "\n",
       "   VENDAS_CICLO    Ciclo  \n",
       "0           1.0  C202413  \n",
       "1          30.0  C202413  \n",
       "2           3.0  C202414  \n",
       "3           2.0  C202505  \n",
       "4          37.0  C202506  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfi = dfi[dfi['SKU2'] == \"-\"]\n",
    "\n",
    "dfi.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PDV</th>\n",
       "      <th>SKU_FINAL</th>\n",
       "      <th>DESCRICAO</th>\n",
       "      <th>C202412</th>\n",
       "      <th>C202413</th>\n",
       "      <th>C202414</th>\n",
       "      <th>C202415</th>\n",
       "      <th>C202416</th>\n",
       "      <th>C202417</th>\n",
       "      <th>C202501</th>\n",
       "      <th>...</th>\n",
       "      <th>C202503</th>\n",
       "      <th>C202504</th>\n",
       "      <th>C202505</th>\n",
       "      <th>C202506</th>\n",
       "      <th>C202507</th>\n",
       "      <th>C202508</th>\n",
       "      <th>C202509</th>\n",
       "      <th>C202510</th>\n",
       "      <th>C202511</th>\n",
       "      <th>C202512</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>12522</td>\n",
       "      <td>1004</td>\n",
       "      <td>FLORATTA DES COL MY BLUE 75ml</td>\n",
       "      <td>0.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>56.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>12522</td>\n",
       "      <td>1080</td>\n",
       "      <td>INTENSE GLOS LAB BUBBAL TUT/FRUT 5ml</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12522</td>\n",
       "      <td>1296</td>\n",
       "      <td>PMPCK THE BLEND DES ANTIT AER 2x75g</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>12522</td>\n",
       "      <td>1302</td>\n",
       "      <td>PMPCK LILY DES ANTIT AER 2x75g</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>12522</td>\n",
       "      <td>1314</td>\n",
       "      <td>PMPCK ZAAD DES ANTIT AER 2x75g</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     PDV  SKU_FINAL                             DESCRICAO  C202412  C202413  \\\n",
       "0  12522       1004         FLORATTA DES COL MY BLUE 75ml      0.0     15.0   \n",
       "1  12522       1080  INTENSE GLOS LAB BUBBAL TUT/FRUT 5ml      0.0      0.0   \n",
       "2  12522       1296   PMPCK THE BLEND DES ANTIT AER 2x75g      0.0      1.0   \n",
       "3  12522       1302        PMPCK LILY DES ANTIT AER 2x75g      1.0      4.0   \n",
       "4  12522       1314        PMPCK ZAAD DES ANTIT AER 2x75g      0.0      2.0   \n",
       "\n",
       "   C202414  C202415  C202416  C202417  C202501  ...  C202503  C202504  \\\n",
       "0      2.0      3.0     56.0      6.0      1.0  ...      1.0      5.0   \n",
       "1      4.0      0.0     10.0      2.0      2.0  ...      2.0      3.0   \n",
       "2      3.0      3.0      2.0      0.0      0.0  ...      0.0      0.0   \n",
       "3      3.0      5.0      6.0      0.0      0.0  ...      0.0      0.0   \n",
       "4      3.0      1.0      1.0      0.0      0.0  ...      0.0      0.0   \n",
       "\n",
       "   C202505  C202506  C202507  C202508  C202509  C202510  C202511  C202512  \n",
       "0     12.0      7.0      2.0      0.0      1.0      2.0      5.0      2.0  \n",
       "1      1.0      7.0     17.0      0.0      0.0      0.0      0.0      0.0  \n",
       "2      0.0      0.0      0.0      0.0      0.0      0.0      0.0      0.0  \n",
       "3      0.0      3.0      4.0      0.0      4.0      4.0      2.0      1.0  \n",
       "4      0.0      0.0      0.0      0.0      0.0      0.0      1.0      1.0  \n",
       "\n",
       "[5 rows x 21 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# 1. Agrupamento\n",
    "df_agrupado = dfi.groupby(['PDV', 'SKU_FINAL', 'DESCRICAO', 'Ciclo'], as_index=False)['VENDAS_CICLO'].sum()\n",
    "\n",
    "# 2. Pivotar o DataFrame\n",
    "df_pivotado = df_agrupado.pivot_table(\n",
    "    index=['PDV', 'SKU_FINAL', 'DESCRICAO'],\n",
    "    columns='Ciclo',\n",
    "    values='VENDAS_CICLO',\n",
    "    fill_value=0  # Substitui NaN por 0\n",
    ")\n",
    "\n",
    "# 3. Resetar o índice para deixar como DataFrame normal (opcional)\n",
    "df_pivotadowawa = df_pivotado.reset_index()\n",
    "\n",
    "# 4. (Opcional) Renomear colunas com prefixo \"Ciclo_\"\n",
    "df_pivotadowawa.columns.name = None\n",
    "df_pivotadowawa = df_pivotadowawa.rename(columns=lambda x: f'Ciclo_{x}' if isinstance(x, (int, str)) and str(x).isdigit() else x)\n",
    "\n",
    "# Resultado final\n",
    "df_pivotadowawa.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PDV</th>\n",
       "      <th>SKU_FINAL</th>\n",
       "      <th>DESCRICAO</th>\n",
       "      <th>C202412</th>\n",
       "      <th>C202413</th>\n",
       "      <th>C202414</th>\n",
       "      <th>C202415</th>\n",
       "      <th>C202416</th>\n",
       "      <th>C202417</th>\n",
       "      <th>C202501</th>\n",
       "      <th>...</th>\n",
       "      <th>C202503</th>\n",
       "      <th>C202504</th>\n",
       "      <th>C202505</th>\n",
       "      <th>C202506</th>\n",
       "      <th>C202507</th>\n",
       "      <th>C202508</th>\n",
       "      <th>C202509</th>\n",
       "      <th>C202510</th>\n",
       "      <th>C202511</th>\n",
       "      <th>C202512</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>0 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [PDV, SKU_FINAL, DESCRICAO, C202412, C202413, C202414, C202415, C202416, C202417, C202501, C202502, C202503, C202504, C202505, C202506, C202507, C202508, C202509, C202510, C202511, C202512]\n",
       "Index: []\n",
       "\n",
       "[0 rows x 21 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_pivotadowawa[(df_pivotadowawa['SKU_FINAL'] == '1634') & (df_pivotadowawa['PDV'] == '20998')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "df_pdv = pd.read_excel(r\"C:\\Users\\joao.herculano\\Documents\\PDV_ATT.xlsx\")\n",
    "\n",
    "df_pdv_origi = pd.read_excel(r\"C:\\Users\\joao.herculano\\Documents\\PDV_ATT.xlsx\")\n",
    "\n",
    "df_pdv = df_pdv.rename(columns={'DESCRIÇÃO':'DESCRIÇÃO PDV'})\n",
    "\n",
    "df_pdv = df_pdv[df_pdv['GESTÃO']!=\"Inativa\"]\n",
    "\n",
    "df_pdv = df_pdv.drop(columns=['REGIÃO', 'ESTADO','CIDADE','GESTÃO', 'STATUS'])\n",
    "\n",
    "df_pdv = df_pdv[df_pdv['CANAL']!='MTZ']\n",
    "\n",
    "#df_pdv['CANAL'] = np.where((df_pdv['CANAL']=='LJ')|(df_pdv['CANAL']=='HIB')|(df_pdv['CANAL']=='CD'),'TODOS','VD')\n",
    "\n",
    "df_pdv['MATCH'] = 1\n",
    "\n",
    "df_pdv = df_pdv[(df_pdv['PDV']!=910291)&(df_pdv['PDV']!=910173)]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PDV</th>\n",
       "      <th>CANAL</th>\n",
       "      <th>DESCRIÇÃO PDV</th>\n",
       "      <th>PDV DESC</th>\n",
       "      <th>UF</th>\n",
       "      <th>MARCA</th>\n",
       "      <th>ANALISTA</th>\n",
       "      <th>SUPERVISOR</th>\n",
       "      <th>MATCH</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>21007</td>\n",
       "      <td>LJ</td>\n",
       "      <td>MOREIRA 2</td>\n",
       "      <td>21007-MOREIRA 2</td>\n",
       "      <td>AL</td>\n",
       "      <td>O BOTICARIO</td>\n",
       "      <td>LUAN</td>\n",
       "      <td>Maxwell Vieira</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      PDV CANAL DESCRIÇÃO PDV         PDV DESC  UF        MARCA ANALISTA  \\\n",
       "43  21007    LJ     MOREIRA 2  21007-MOREIRA 2  AL  O BOTICARIO     LUAN   \n",
       "\n",
       "        SUPERVISOR  MATCH  \n",
       "43  Maxwell Vieira      1  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_pdv[df_pdv['PDV']==21007]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_pdv = df_pdv.drop(columns=['PDV DESC'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\joao.herculano\\AppData\\Local\\Temp\\ipykernel_2580\\268247862.py:25: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 connection. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.\n",
      "  df_draft_ = pd.read_sql(query_est_draft, conn)\n"
     ]
    }
   ],
   "source": [
    "conn = pyodbc.connect(\n",
    "    f\"DRIVER={{SQL Server}};\"\n",
    "    f\"SERVER={config['banco']['host']},1433;\"\n",
    "    f\"DATABASE=GINSENG;\"\n",
    "    f\"UID={config['banco']['user']};\"\n",
    "    f\"PWD={config['banco']['password']}\"\n",
    ")\n",
    "\n",
    "query_est_draft = f'''\n",
    "select em.PDV ,\n",
    "COALESCE(em.SKU_PARA , em.SKU) as SKU,\n",
    "em.DESCRICAO,\n",
    "d.salescurve as curva,\n",
    "d.codcategory as categoria,\n",
    "d.nextcycleprojection as proj_mar\n",
    ",d.secondtonextcycleprojection as 'proj_mar+1',\n",
    "em.[ESTOQUE ATUAL],em.[ESTOQUE EM TRANSITO],\n",
    "em.[PEDIDO PENDENTE],\n",
    "em.[DDV PREVISTO] ,\n",
    "d.pricesellin as 'preço'\n",
    "from estoque_mar em \n",
    "left join draft d on d.code = COALESCE(em.SKU_PARA , em.SKU) and d.loja_id = em.PDV \n",
    "where em.ORIGEM = 'BOT'\n",
    "'''\n",
    "df_draft_ = pd.read_sql(query_est_draft, conn)\n",
    "conn.close()\n",
    "\n",
    "#FALTA ATUALIZAR QUERY"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_draft = df_draft_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PDV</th>\n",
       "      <th>SKU</th>\n",
       "      <th>DESCRICAO</th>\n",
       "      <th>curva</th>\n",
       "      <th>categoria</th>\n",
       "      <th>proj_mar</th>\n",
       "      <th>proj_mar+1</th>\n",
       "      <th>ESTOQUE ATUAL</th>\n",
       "      <th>ESTOQUE EM TRANSITO</th>\n",
       "      <th>PEDIDO PENDENTE</th>\n",
       "      <th>DDV PREVISTO</th>\n",
       "      <th>preço</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>128590</th>\n",
       "      <td>24293</td>\n",
       "      <td>1634</td>\n",
       "      <td>NSPA GEL ESF CPO MORANGO RUBY 175G</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>128840</th>\n",
       "      <td>21278</td>\n",
       "      <td>1634</td>\n",
       "      <td>NSPA GEL ESF CPO MORANGO RUBY 175G</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>128956</th>\n",
       "      <td>12826</td>\n",
       "      <td>1634</td>\n",
       "      <td>NSPA GEL ESF CPO MORANGO RUBY 175G</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>129361</th>\n",
       "      <td>4560</td>\n",
       "      <td>1634</td>\n",
       "      <td>NSPA GEL ESF CPO MORANGO RUBY 175G</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>129451</th>\n",
       "      <td>5699</td>\n",
       "      <td>1634</td>\n",
       "      <td>NSPA GEL ESF CPO MORANGO RUBY 175G</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0,05</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>337558</th>\n",
       "      <td>21647</td>\n",
       "      <td>1634</td>\n",
       "      <td>NSPA GEL ESF CPO MORANGO RUBY 175G</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>345348</th>\n",
       "      <td>20009</td>\n",
       "      <td>1634</td>\n",
       "      <td>NSPA GEL ESF CPO MORANGO RUBY 175G</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>349112</th>\n",
       "      <td>20968</td>\n",
       "      <td>1634</td>\n",
       "      <td>NSPA GEL ESF CPO MORANGO RUBY 175G</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>358656</th>\n",
       "      <td>12830</td>\n",
       "      <td>1634</td>\n",
       "      <td>NSPA GEL ESF CPO MORANGO RUBY 175G</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>359288</th>\n",
       "      <td>20997</td>\n",
       "      <td>1634</td>\n",
       "      <td>NSPA GEL ESF CPO MORANGO RUBY 175G</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>73 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          PDV   SKU                           DESCRICAO curva categoria  \\\n",
       "128590  24293  1634  NSPA GEL ESF CPO MORANGO RUBY 175G  None      None   \n",
       "128840  21278  1634  NSPA GEL ESF CPO MORANGO RUBY 175G  None      None   \n",
       "128956  12826  1634  NSPA GEL ESF CPO MORANGO RUBY 175G  None      None   \n",
       "129361   4560  1634  NSPA GEL ESF CPO MORANGO RUBY 175G  None      None   \n",
       "129451   5699  1634  NSPA GEL ESF CPO MORANGO RUBY 175G  None      None   \n",
       "...       ...   ...                                 ...   ...       ...   \n",
       "337558  21647  1634  NSPA GEL ESF CPO MORANGO RUBY 175G  None      None   \n",
       "345348  20009  1634  NSPA GEL ESF CPO MORANGO RUBY 175G  None      None   \n",
       "349112  20968  1634  NSPA GEL ESF CPO MORANGO RUBY 175G  None      None   \n",
       "358656  12830  1634  NSPA GEL ESF CPO MORANGO RUBY 175G  None      None   \n",
       "359288  20997  1634  NSPA GEL ESF CPO MORANGO RUBY 175G  None      None   \n",
       "\n",
       "        proj_mar  proj_mar+1 ESTOQUE ATUAL ESTOQUE EM TRANSITO  \\\n",
       "128590       NaN         NaN             3                   0   \n",
       "128840       NaN         NaN             2                   0   \n",
       "128956       NaN         NaN             3                   0   \n",
       "129361       NaN         NaN             5                   0   \n",
       "129451       NaN         NaN             4                   0   \n",
       "...          ...         ...           ...                 ...   \n",
       "337558       NaN         NaN             4                   0   \n",
       "345348       NaN         NaN            10                   0   \n",
       "349112       NaN         NaN             8                   0   \n",
       "358656       NaN         NaN             5                   0   \n",
       "359288       NaN         NaN             3                   0   \n",
       "\n",
       "       PEDIDO PENDENTE DDV PREVISTO  preço  \n",
       "128590               0            0    NaN  \n",
       "128840               0            0    NaN  \n",
       "128956               0            0    NaN  \n",
       "129361               0            0    NaN  \n",
       "129451               0         0,05    NaN  \n",
       "...                ...          ...    ...  \n",
       "337558               0            0    NaN  \n",
       "345348               0            0    NaN  \n",
       "349112               0            0    NaN  \n",
       "358656               0            0    NaN  \n",
       "359288               0            0    NaN  \n",
       "\n",
       "[73 rows x 12 columns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_draft[df_draft['SKU']=='1634']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(350945, 12)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_draft = df_draft[~df_draft['DESCRICAO'].str.contains('OUI', na=False)]\n",
    "\n",
    "df_draft.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\joao.herculano\\AppData\\Local\\Temp\\ipykernel_2580\\1765304903.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df_draft['ESTOQUE ATUAL'] = df_draft['ESTOQUE ATUAL'].astype(float)\n",
      "C:\\Users\\joao.herculano\\AppData\\Local\\Temp\\ipykernel_2580\\1765304903.py:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df_draft['ESTOQUE EM TRANSITO'] = df_draft['ESTOQUE EM TRANSITO'].astype(float)\n",
      "C:\\Users\\joao.herculano\\AppData\\Local\\Temp\\ipykernel_2580\\1765304903.py:5: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df_draft['PEDIDO PENDENTE'] = df_draft['PEDIDO PENDENTE'].astype(float)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "PDV                     object\n",
       "SKU                     object\n",
       "DESCRICAO               object\n",
       "curva                   object\n",
       "categoria               object\n",
       "proj_mar               float64\n",
       "proj_mar+1             float64\n",
       "ESTOQUE ATUAL          float64\n",
       "ESTOQUE EM TRANSITO    float64\n",
       "PEDIDO PENDENTE        float64\n",
       "DDV PREVISTO            object\n",
       "preço                  float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_draft['ESTOQUE ATUAL'] = df_draft['ESTOQUE ATUAL'].astype(float)\n",
    "\n",
    "df_draft['ESTOQUE EM TRANSITO'] = df_draft['ESTOQUE EM TRANSITO'].astype(float)\n",
    "\n",
    "df_draft['PEDIDO PENDENTE'] = df_draft['PEDIDO PENDENTE'].astype(float)\n",
    "\n",
    "df_draft.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\joao.herculano\\AppData\\Local\\Temp\\ipykernel_2580\\1952797845.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df_draft['DDV PREVISTO'] = df_draft['DDV PREVISTO'].str.replace(',','.')\n",
      "C:\\Users\\joao.herculano\\AppData\\Local\\Temp\\ipykernel_2580\\1952797845.py:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df_draft['DDV PREVISTO'] = df_draft['DDV PREVISTO'].astype(float)\n",
      "C:\\Users\\joao.herculano\\AppData\\Local\\Temp\\ipykernel_2580\\1952797845.py:5: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df_draft['DDV PREVISTO'] = df_draft['DDV PREVISTO'].fillna(0)\n"
     ]
    }
   ],
   "source": [
    "df_draft['DDV PREVISTO'] = df_draft['DDV PREVISTO'].str.replace(',','.')\n",
    "\n",
    "df_draft['DDV PREVISTO'] = df_draft['DDV PREVISTO'].astype(float)\n",
    "\n",
    "df_draft['DDV PREVISTO'] = df_draft['DDV PREVISTO'].fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_draft = df_draft.groupby(['PDV','SKU','curva','categoria'])[['proj_mar','proj_mar+1','ESTOQUE ATUAL','ESTOQUE EM TRANSITO','PEDIDO PENDENTE','DDV PREVISTO','preço']].max().reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_tabela = df_tabela.drop_duplicates()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_bi_preco = pd.read_excel(r\"C:\\Users\\joao.herculano\\GRUPO GINSENG\\Assistência Suprimentos - 2025\\SUPRIMENTOS\\DB_PROMOÇÕES\\BOTICARIO\\C13\\preço BI\\TABELA DE PREÇOS (2).xlsx\")\n",
    "\n",
    "df_bi_preco = df_bi_preco.drop(columns=['Tipo Preço','CATEGORIA','LINHA','MARCA'])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_pivotadowawa['SKU_FINAL'] = df_pivotadowawa['SKU_FINAL'].astype('str')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1004\n",
       "1    1080\n",
       "2    1296\n",
       "3    1302\n",
       "4    1314\n",
       "Name: SKU_FINAL, dtype: object"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_pivotadowawa['SKU_FINAL'] = df_pivotadowawa['SKU_FINAL'].astype(str)\n",
    "\n",
    "df_pivotadowawa['SKU_FINAL'].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PDV</th>\n",
       "      <th>SKU</th>\n",
       "      <th>curva</th>\n",
       "      <th>categoria</th>\n",
       "      <th>proj_mar</th>\n",
       "      <th>proj_mar+1</th>\n",
       "      <th>ESTOQUE ATUAL</th>\n",
       "      <th>ESTOQUE EM TRANSITO</th>\n",
       "      <th>PEDIDO PENDENTE</th>\n",
       "      <th>DDV PREVISTO</th>\n",
       "      <th>preço</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [PDV, SKU, curva, categoria, proj_mar, proj_mar+1, ESTOQUE ATUAL, ESTOQUE EM TRANSITO, PEDIDO PENDENTE, DDV PREVISTO, preço]\n",
       "Index: []"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_draft[df_draft['SKU']=='1634']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_draft = pd.merge(df_draft,df_pivotadowawa,left_on=['PDV','SKU'],right_on=['PDV','SKU_FINAL'],how='inner')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PDV</th>\n",
       "      <th>SKU</th>\n",
       "      <th>curva</th>\n",
       "      <th>categoria</th>\n",
       "      <th>proj_mar</th>\n",
       "      <th>proj_mar+1</th>\n",
       "      <th>ESTOQUE ATUAL</th>\n",
       "      <th>ESTOQUE EM TRANSITO</th>\n",
       "      <th>PEDIDO PENDENTE</th>\n",
       "      <th>DDV PREVISTO</th>\n",
       "      <th>...</th>\n",
       "      <th>C202503</th>\n",
       "      <th>C202504</th>\n",
       "      <th>C202505</th>\n",
       "      <th>C202506</th>\n",
       "      <th>C202507</th>\n",
       "      <th>C202508</th>\n",
       "      <th>C202509</th>\n",
       "      <th>C202510</th>\n",
       "      <th>C202511</th>\n",
       "      <th>C202512</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>0 rows × 31 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [PDV, SKU, curva, categoria, proj_mar, proj_mar+1, ESTOQUE ATUAL, ESTOQUE EM TRANSITO, PEDIDO PENDENTE, DDV PREVISTO, preço, SKU_FINAL, DESCRICAO, C202412, C202413, C202414, C202415, C202416, C202417, C202501, C202502, C202503, C202504, C202505, C202506, C202507, C202508, C202509, C202510, C202511, C202512]\n",
       "Index: []\n",
       "\n",
       "[0 rows x 31 columns]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_draft[df_draft['SKU']=='1634']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(34265, 31)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final = pd.merge(left=df_tabela,right=df_pdv,on='MATCH',how='left')\n",
    "\n",
    "df_final = df_final.drop_duplicates()\n",
    "\n",
    "df_final.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PDV\n",
       "12522    445\n",
       "12817    445\n",
       "12818    445\n",
       "12820    445\n",
       "12823    445\n",
       "        ... \n",
       "24257    445\n",
       "24268    445\n",
       "24269    445\n",
       "24293    445\n",
       "23813    445\n",
       "Name: count, Length: 77, dtype: int64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final['PDV'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PDV</th>\n",
       "      <th>SKU</th>\n",
       "      <th>curva</th>\n",
       "      <th>categoria</th>\n",
       "      <th>proj_mar</th>\n",
       "      <th>proj_mar+1</th>\n",
       "      <th>ESTOQUE ATUAL</th>\n",
       "      <th>ESTOQUE EM TRANSITO</th>\n",
       "      <th>PEDIDO PENDENTE</th>\n",
       "      <th>DDV PREVISTO</th>\n",
       "      <th>...</th>\n",
       "      <th>C202503</th>\n",
       "      <th>C202504</th>\n",
       "      <th>C202505</th>\n",
       "      <th>C202506</th>\n",
       "      <th>C202507</th>\n",
       "      <th>C202508</th>\n",
       "      <th>C202509</th>\n",
       "      <th>C202510</th>\n",
       "      <th>C202511</th>\n",
       "      <th>C202512</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>74681</th>\n",
       "      <td>3546</td>\n",
       "      <td>52627</td>\n",
       "      <td>B</td>\n",
       "      <td>CUIDADOS FACIAIS</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.09</td>\n",
       "      <td>...</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>74682</th>\n",
       "      <td>3546</td>\n",
       "      <td>52627</td>\n",
       "      <td>B</td>\n",
       "      <td>CUIDADOS FACIAIS</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.09</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 31 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        PDV    SKU curva         categoria  proj_mar  proj_mar+1  \\\n",
       "74681  3546  52627     B  CUIDADOS FACIAIS       2.0         2.0   \n",
       "74682  3546  52627     B  CUIDADOS FACIAIS       2.0         2.0   \n",
       "\n",
       "       ESTOQUE ATUAL  ESTOQUE EM TRANSITO  PEDIDO PENDENTE  DDV PREVISTO  ...  \\\n",
       "74681            0.0                  2.0              0.0          0.09  ...   \n",
       "74682            0.0                  2.0              0.0          0.09  ...   \n",
       "\n",
       "       C202503 C202504 C202505  C202506  C202507  C202508  C202509  C202510  \\\n",
       "74681      5.0     0.0     0.0      0.0      0.0      0.0      0.0      0.0   \n",
       "74682      0.0     0.0     4.0      3.0      0.0      3.0      4.0      1.0   \n",
       "\n",
       "       C202511  C202512  \n",
       "74681      0.0      0.0  \n",
       "74682      3.0      3.0  \n",
       "\n",
       "[2 rows x 31 columns]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_draft[(df_draft['SKU'] == '52627') & (df_draft['PDV'] == '3546') ].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['PDV', 'SKU', 'curva', 'categoria', 'proj_mar', 'proj_mar+1',\n",
       "       'ESTOQUE ATUAL', 'ESTOQUE EM TRANSITO', 'PEDIDO PENDENTE',\n",
       "       'DDV PREVISTO', 'preço', 'SKU_FINAL', 'C202412', 'C202413', 'C202414',\n",
       "       'C202415', 'C202416', 'C202417', 'C202501', 'C202502', 'C202503',\n",
       "       'C202504', 'C202505', 'C202506', 'C202507', 'C202508', 'C202509',\n",
       "       'C202510', 'C202511', 'C202512'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_draft = df_draft.drop(columns='DESCRICAO')\n",
    "\n",
    "df_draft.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PDV\n",
       "20998    1209\n",
       "23711    1190\n",
       "20997    1182\n",
       "20993    1172\n",
       "20996    1161\n",
       "         ... \n",
       "24269     490\n",
       "24253     472\n",
       "24268     452\n",
       "24254     439\n",
       "24258     336\n",
       "Name: count, Length: 75, dtype: int64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numericos = df_draft.columns[-18:]\n",
    "\n",
    "chaves = [col for col in df_draft.columns if col not in numericos]\n",
    "\n",
    "# 2. Fazer o groupby\n",
    "df_draft_agg = (\n",
    "    df_draft\n",
    "    .groupby(chaves, as_index=False)[numericos]\n",
    "    .max()\n",
    ")\n",
    "\n",
    "df_draft_agg['PDV'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PDV</th>\n",
       "      <th>SKU</th>\n",
       "      <th>curva</th>\n",
       "      <th>categoria</th>\n",
       "      <th>proj_mar</th>\n",
       "      <th>proj_mar+1</th>\n",
       "      <th>ESTOQUE ATUAL</th>\n",
       "      <th>ESTOQUE EM TRANSITO</th>\n",
       "      <th>PEDIDO PENDENTE</th>\n",
       "      <th>DDV PREVISTO</th>\n",
       "      <th>...</th>\n",
       "      <th>C202503</th>\n",
       "      <th>C202504</th>\n",
       "      <th>C202505</th>\n",
       "      <th>C202506</th>\n",
       "      <th>C202507</th>\n",
       "      <th>C202508</th>\n",
       "      <th>C202509</th>\n",
       "      <th>C202510</th>\n",
       "      <th>C202511</th>\n",
       "      <th>C202512</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>0 rows × 30 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [PDV, SKU, curva, categoria, proj_mar, proj_mar+1, ESTOQUE ATUAL, ESTOQUE EM TRANSITO, PEDIDO PENDENTE, DDV PREVISTO, preço, SKU_FINAL, C202412, C202413, C202414, C202415, C202416, C202417, C202501, C202502, C202503, C202504, C202505, C202506, C202507, C202508, C202509, C202510, C202511, C202512]\n",
       "Index: []\n",
       "\n",
       "[0 rows x 30 columns]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_draft[df_draft['SKU']==1634]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_draft1 = df_draft_agg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['PDV', 'SKU', 'curva', 'categoria', 'proj_mar', 'proj_mar+1',\n",
       "       'ESTOQUE ATUAL', 'ESTOQUE EM TRANSITO', 'PEDIDO PENDENTE',\n",
       "       'DDV PREVISTO', 'preço', 'SKU_FINAL', 'C202412', 'C202413', 'C202414',\n",
       "       'C202415', 'C202416', 'C202417', 'C202501', 'C202502', 'C202503',\n",
       "       'C202504', 'C202505', 'C202506', 'C202507', 'C202508', 'C202509',\n",
       "       'C202510', 'C202511', 'C202512'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_draft1.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_draft1.to_clipboard()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(34265, 60)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final['PDV'] = df_final['PDV'].astype('str')\n",
    "df_final['Código'] = df_final['Código'].astype('str')\n",
    "\n",
    "\n",
    "df_final = pd.merge(left=df_final,right=df_draft1,left_on=['PDV','Código'],right_on=['PDV','SKU'],how='left')\n",
    "df_final.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Ciclo</th>\n",
       "      <th>Região</th>\n",
       "      <th>Canal</th>\n",
       "      <th>Código</th>\n",
       "      <th>Descrição</th>\n",
       "      <th>IAF</th>\n",
       "      <th>Tipo de pedido</th>\n",
       "      <th>Foco</th>\n",
       "      <th>Unidade de negócio</th>\n",
       "      <th>Marca</th>\n",
       "      <th>...</th>\n",
       "      <th>C202503</th>\n",
       "      <th>C202504</th>\n",
       "      <th>C202505</th>\n",
       "      <th>C202506</th>\n",
       "      <th>C202507</th>\n",
       "      <th>C202508</th>\n",
       "      <th>C202509</th>\n",
       "      <th>C202510</th>\n",
       "      <th>C202511</th>\n",
       "      <th>C202512</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>12936</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>1634</td>\n",
       "      <td>NSPA GEL ESF CPO MORANGO RUBY 175G</td>\n",
       "      <td>Não</td>\n",
       "      <td>Especial</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>NATIVA SPA</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12937</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>1634</td>\n",
       "      <td>NSPA GEL ESF CPO MORANGO RUBY 175G</td>\n",
       "      <td>Não</td>\n",
       "      <td>Especial</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>NATIVA SPA</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12938</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>1634</td>\n",
       "      <td>NSPA GEL ESF CPO MORANGO RUBY 175G</td>\n",
       "      <td>Não</td>\n",
       "      <td>Especial</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>NATIVA SPA</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12939</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>1634</td>\n",
       "      <td>NSPA GEL ESF CPO MORANGO RUBY 175G</td>\n",
       "      <td>Não</td>\n",
       "      <td>Especial</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>NATIVA SPA</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12940</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>1634</td>\n",
       "      <td>NSPA GEL ESF CPO MORANGO RUBY 175G</td>\n",
       "      <td>Não</td>\n",
       "      <td>Especial</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>NATIVA SPA</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 60 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        Ciclo Região Canal Código                           Descrição  IAF  \\\n",
       "12936  202515    NNE  Loja   1634  NSPA GEL ESF CPO MORANGO RUBY 175G  Não   \n",
       "12937  202515    NNE  Loja   1634  NSPA GEL ESF CPO MORANGO RUBY 175G  Não   \n",
       "12938  202515    NNE  Loja   1634  NSPA GEL ESF CPO MORANGO RUBY 175G  Não   \n",
       "12939  202515    NNE  Loja   1634  NSPA GEL ESF CPO MORANGO RUBY 175G  Não   \n",
       "12940  202515    NNE  Loja   1634  NSPA GEL ESF CPO MORANGO RUBY 175G  Não   \n",
       "\n",
       "      Tipo de pedido Foco Unidade de negócio       Marca  ... C202503 C202504  \\\n",
       "12936       Especial  Não                BOT  NATIVA SPA  ...     NaN     NaN   \n",
       "12937       Especial  Não                BOT  NATIVA SPA  ...     NaN     NaN   \n",
       "12938       Especial  Não                BOT  NATIVA SPA  ...     NaN     NaN   \n",
       "12939       Especial  Não                BOT  NATIVA SPA  ...     NaN     NaN   \n",
       "12940       Especial  Não                BOT  NATIVA SPA  ...     NaN     NaN   \n",
       "\n",
       "       C202505 C202506 C202507 C202508 C202509 C202510 C202511 C202512  \n",
       "12936      NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN  \n",
       "12937      NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN  \n",
       "12938      NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN  \n",
       "12939      NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN  \n",
       "12940      NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN  \n",
       "\n",
       "[5 rows x 60 columns]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final[df_final['Código']=='1634'].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PDV\n",
       "12522    445\n",
       "12817    445\n",
       "12818    445\n",
       "12820    445\n",
       "12823    445\n",
       "        ... \n",
       "24257    445\n",
       "24268    445\n",
       "24269    445\n",
       "24293    445\n",
       "23813    445\n",
       "Name: count, Length: 77, dtype: int64"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final['PDV'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final = df_final.drop(columns=['Sortimento P', 'Sortimento M',\n",
    "       'Sortimento G','MARCA'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "calendario = pd.read_excel(r\"C:\\Users\\joao.herculano\\GRUPO GINSENG\\Assistência Suprimentos - 2025\\SUPRIMENTOS\\BD_LANÇAMENTOS\\BASE DE DADOS LANÇAMENTO\\BOT\\CICLO 9\\CALENDARIO_CICLO\\Ciclo_Expandido_com_Datas.xlsx\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Ciclo</th>\n",
       "      <th>INICIO CICLO</th>\n",
       "      <th>FIM CICLO</th>\n",
       "      <th>DURAÇÃO</th>\n",
       "      <th>Date</th>\n",
       "      <th>NUM_CICLO</th>\n",
       "      <th>ANO_CICLO</th>\n",
       "      <th>CICLOMAIS2</th>\n",
       "      <th>dias_ate_inicio</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2367</th>\n",
       "      <td>C202514</td>\n",
       "      <td>2025-09-22</td>\n",
       "      <td>2025-10-12</td>\n",
       "      <td>21</td>\n",
       "      <td>2025-09-22</td>\n",
       "      <td>14</td>\n",
       "      <td>C2025</td>\n",
       "      <td>C202516</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Ciclo INICIO CICLO  FIM CICLO  DURAÇÃO       Date  NUM_CICLO  \\\n",
       "2367  C202514   2025-09-22 2025-10-12       21 2025-09-22         14   \n",
       "\n",
       "     ANO_CICLO CICLOMAIS2  dias_ate_inicio  \n",
       "2367     C2025    C202516               25  "
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "calendario['Date'] = pd.to_datetime(calendario['Date'])\n",
    "\n",
    "# Get today (normalized to midnight)\n",
    "today = pd.Timestamp(\"today\").normalize()\n",
    "\n",
    "calendario['NUM_CICLO'] = calendario['Ciclo'].str[-2:].astype(int)\n",
    "calendario['ANO_CICLO'] = calendario['Ciclo'].str[0:5]\n",
    "\n",
    "\n",
    "calendario = calendario[calendario['MARCA'] == \"BOTICARIO\"]\n",
    "\n",
    "calendario = calendario.drop(columns='MARCA')\n",
    "\n",
    "calendario['CICLOMAIS2'] = calendario['ANO_CICLO'].astype(str) + (calendario['NUM_CICLO'].astype(int) + 2).astype(str).str.zfill(2) # >>>>>>>>> MUDAR PRA CICLO CORRETO \n",
    "\n",
    "ciclo_mais2 = calendario[calendario['Date'].dt.normalize() == today]['CICLOMAIS2'].iloc[0]\n",
    "\n",
    "\n",
    "# Filter rows where date matches today\n",
    "filtered_calendario = calendario[calendario['Ciclo'] == ciclo_mais2][:1]\n",
    "\n",
    "\n",
    "filtered_calendario['dias_ate_inicio'] = filtered_calendario['INICIO CICLO'].iloc[0] - today\n",
    "\n",
    "filtered_calendario['dias_ate_inicio'] = filtered_calendario['dias_ate_inicio'].dt.days.astype(int)\n",
    "\n",
    "filtered_calendario\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "filtered_calendario['MATCH'] = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_pdv['UF'] = np.where(df_pdv['UF'] == 'VDC','BA',df_pdv['UF'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_draft['PDV'] = df_draft['PDV'].astype(str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(34265, 61)"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final = pd.merge(left=df_final, right=filtered_calendario[['Ciclo','INICIO CICLO','FIM CICLO','DURAÇÃO','MATCH','dias_ate_inicio']], on='MATCH',how='left')\n",
    "df_final.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final['Código'] = df_final['Código'].astype('Int64') \n",
    "df_final['PDV'] = df_final['PDV'].astype('Int64') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SKU1</th>\n",
       "      <th>SKU2</th>\n",
       "      <th>Descrição</th>\n",
       "      <th>UF</th>\n",
       "      <th>PC</th>\n",
       "      <th>PV</th>\n",
       "      <th>chave sku2</th>\n",
       "      <th>chave sku1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>84918</td>\n",
       "      <td>84918</td>\n",
       "      <td>ESTJ BOTI CALENDARIO ADVENTO NAT/24</td>\n",
       "      <td>AL</td>\n",
       "      <td>393.70</td>\n",
       "      <td>1300.0</td>\n",
       "      <td>AL84918</td>\n",
       "      <td>AL84918</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>84918</td>\n",
       "      <td>84918</td>\n",
       "      <td>ESTJ BOTI CALENDARIO ADVENTO NAT/24</td>\n",
       "      <td>BA</td>\n",
       "      <td>436.73</td>\n",
       "      <td>1300.0</td>\n",
       "      <td>BA84918</td>\n",
       "      <td>BA84918</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>84918</td>\n",
       "      <td>84918</td>\n",
       "      <td>ESTJ BOTI CALENDARIO ADVENTO NAT/24</td>\n",
       "      <td>SE</td>\n",
       "      <td>379.94</td>\n",
       "      <td>1300.0</td>\n",
       "      <td>SE84918</td>\n",
       "      <td>SE84918</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>54230</td>\n",
       "      <td>54230</td>\n",
       "      <td>MALBEC DES COL 20 ANOS 100ml</td>\n",
       "      <td>AL</td>\n",
       "      <td>138.54</td>\n",
       "      <td>499.9</td>\n",
       "      <td>AL54230</td>\n",
       "      <td>AL54230</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>54230</td>\n",
       "      <td>54230</td>\n",
       "      <td>MALBEC DES COL 20 ANOS 100ml</td>\n",
       "      <td>BA</td>\n",
       "      <td>148.66</td>\n",
       "      <td>499.9</td>\n",
       "      <td>BA54230</td>\n",
       "      <td>BA54230</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    SKU1   SKU2                            Descrição  UF      PC      PV  \\\n",
       "0  84918  84918  ESTJ BOTI CALENDARIO ADVENTO NAT/24  AL  393.70  1300.0   \n",
       "1  84918  84918  ESTJ BOTI CALENDARIO ADVENTO NAT/24  BA  436.73  1300.0   \n",
       "2  84918  84918  ESTJ BOTI CALENDARIO ADVENTO NAT/24  SE  379.94  1300.0   \n",
       "3  54230  54230         MALBEC DES COL 20 ANOS 100ml  AL  138.54   499.9   \n",
       "4  54230  54230         MALBEC DES COL 20 ANOS 100ml  BA  148.66   499.9   \n",
       "\n",
       "  chave sku2 chave sku1  \n",
       "0    AL84918    AL84918  \n",
       "1    BA84918    BA84918  \n",
       "2    SE84918    SE84918  \n",
       "3    AL54230    AL54230  \n",
       "4    BA54230    BA54230  "
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_bi_preco['SKU2'] = df_bi_preco['SKU2'].astype('Int64')\n",
    "df_bi_preco.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(34265, 68)"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "\n",
    "df_final = pd.merge(left=df_final,right=df_bi_preco,right_on=['UF','SKU2'],left_on=['UF','Código'],how='left')\n",
    "df_final.shape   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_bi_preco['SKU1'] = df_bi_preco['SKU1'].fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(34265, 72)\n"
     ]
    }
   ],
   "source": [
    "# Conversão segura para Int64 (nullable)\n",
    "df_bi_preco['SKU1'] = pd.to_numeric(df_bi_preco['SKU1'], errors='coerce').astype('Int64')\n",
    "\n",
    "# Realiza o merge\n",
    "df_final = pd.merge(\n",
    "    left=df_final,\n",
    "    right=df_bi_preco[['SKU1', 'SKU2', 'UF', 'PC', 'PV']],\n",
    "    right_on=['UF', 'SKU1'],\n",
    "    left_on=['UF', 'Código'],\n",
    "    how='left'\n",
    ")\n",
    "\n",
    "# Verifica o resultado\n",
    "print(df_final.shape)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final['PRECO DE COMPRA'] = np.where(~df_final['PC_x'].isna(),df_final['PC_x'],df_final['PC_y'])\n",
    "\n",
    "df_final['PRECO DE VENDA'] = np.where(~df_final['PV_x'].isna(),df_final['PV_x'],df_final['PV_y'])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CANAL\n",
       "TODOS    27145\n",
       "VD        7120\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final['CANAL'] = np.where((df_final['CANAL'] == 'LJ') | (df_final['CANAL'] == 'HIB'), \"TODOS\" , np.where((df_final['CANAL'] == 'CD') | (df_final['CANAL'] == 'VD'), \"VD\", df_final['CANAL']))\n",
    "\n",
    "df_final['CANAL'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final = df_final.drop_duplicates()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final = df_final.drop(columns=['SKU1_x','SKU1_y','SKU2_x','SKU2_y','PC_x', 'PV_x','PC_y', 'PV_y','Subcategoria',\n",
    "'Quantidade por caixa'\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final['UFPRODUTO'] = df_final['UF'].astype(str) + df_final['Código'].astype(str)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final = df_final.rename(columns={'Compra inteligente Próximo Ciclo + 1':'Compra inteligente Próximo Ciclo','Arquivo_Origem': 'MARCA'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(0)"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final = df_final[~df_final['Marca'].isna()]\n",
    "df_final['Marca'].isna().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Ciclo_x', 'Região', 'Canal', 'Código', 'Descrição_x', 'IAF',\n",
       "       'Tipo de pedido', 'Foco', 'Unidade de negócio', 'Marca', 'Categoria',\n",
       "       'Tipo de promoção', 'Catálogo', 'Tipo de produto', 'Ação consumidor',\n",
       "       'Percentual de desconto consumidor', 'Ação revendedor',\n",
       "       'Percentual de desconto revendedor', 'MATCH', 'PDV', 'CANAL',\n",
       "       'DESCRIÇÃO PDV', 'UF', 'ANALISTA', 'SUPERVISOR', 'SKU', 'curva',\n",
       "       'categoria', 'proj_mar', 'proj_mar+1', 'ESTOQUE ATUAL',\n",
       "       'ESTOQUE EM TRANSITO', 'PEDIDO PENDENTE', 'DDV PREVISTO', 'preço',\n",
       "       'SKU_FINAL', 'C202412', 'C202413', 'C202414', 'C202415', 'C202416',\n",
       "       'C202417', 'C202501', 'C202502', 'C202503', 'C202504', 'C202505',\n",
       "       'C202506', 'C202507', 'C202508', 'C202509', 'C202510', 'C202511',\n",
       "       'C202512', 'Ciclo_y', 'INICIO CICLO', 'FIM CICLO', 'DURAÇÃO',\n",
       "       'dias_ate_inicio', 'Descrição_y', 'chave sku2', 'chave sku1',\n",
       "       'PRECO DE COMPRA', 'PRECO DE VENDA', 'UFPRODUTO'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_vdc = pd.read_csv(r\"C:\\Users\\joao.herculano\\GRUPO GINSENG\\Assistência Suprimentos - 2025\\SUPRIMENTOS\\DB_PROMOÇÕES\\BOTICARIO\\C10\\VENDA VITORIA 2024\\VENDA VITORIA.csv\")\n",
    "\n",
    "df_vdc['PRODUTO'] = df_vdc['PRODUTO'].astype('Int64')\n",
    "\n",
    "df_final['Código'] = df_final['Código'].astype('Int64')\n",
    "\n",
    "df_final =pd.merge(left=df_final,right=df_vdc,left_on= ['PDV','Código'],right_on= ['PDV GINSENG','PRODUTO'],how='left' )\n",
    "\n",
    "df_final[df_final.columns[-1]] = df_final[df_final.columns[-1]].fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PDV GINSENG</th>\n",
       "      <th>PRODUTO</th>\n",
       "      <th>202401</th>\n",
       "      <th>202402</th>\n",
       "      <th>202403</th>\n",
       "      <th>202404</th>\n",
       "      <th>202405</th>\n",
       "      <th>202406</th>\n",
       "      <th>202407</th>\n",
       "      <th>202408</th>\n",
       "      <th>202409</th>\n",
       "      <th>202410</th>\n",
       "      <th>202411</th>\n",
       "      <th>202412</th>\n",
       "      <th>202413</th>\n",
       "      <th>202414</th>\n",
       "      <th>202415</th>\n",
       "      <th>202416</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1235</th>\n",
       "      <td>23701</td>\n",
       "      <td>74043</td>\n",
       "      <td>12</td>\n",
       "      <td>24</td>\n",
       "      <td>12</td>\n",
       "      <td>16</td>\n",
       "      <td>8</td>\n",
       "      <td>14</td>\n",
       "      <td>6</td>\n",
       "      <td>14</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>10</td>\n",
       "      <td>4</td>\n",
       "      <td>42</td>\n",
       "      <td>0</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3033</th>\n",
       "      <td>23702</td>\n",
       "      <td>74043</td>\n",
       "      <td>2</td>\n",
       "      <td>22</td>\n",
       "      <td>8</td>\n",
       "      <td>22</td>\n",
       "      <td>10</td>\n",
       "      <td>14</td>\n",
       "      <td>14</td>\n",
       "      <td>10</td>\n",
       "      <td>12</td>\n",
       "      <td>8</td>\n",
       "      <td>22</td>\n",
       "      <td>6</td>\n",
       "      <td>14</td>\n",
       "      <td>36</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5171</th>\n",
       "      <td>23703</td>\n",
       "      <td>74043</td>\n",
       "      <td>10</td>\n",
       "      <td>18</td>\n",
       "      <td>6</td>\n",
       "      <td>18</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>2</td>\n",
       "      <td>22</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>26</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      PDV GINSENG  PRODUTO  202401  202402  202403  202404  202405  202406  \\\n",
       "1235        23701    74043      12      24      12      16       8      14   \n",
       "3033        23702    74043       2      22       8      22      10      14   \n",
       "5171        23703    74043      10      18       6      18       6       2   \n",
       "\n",
       "      202407  202408  202409  202410  202411  202412  202413  202414  202415  \\\n",
       "1235       6      14      12       0       8      10       4      42       0   \n",
       "3033      14      10      12       8      22       6      14      36      10   \n",
       "5171      20       0      10       2      22       4       2      26       2   \n",
       "\n",
       "      202416  \n",
       "1235      14  \n",
       "3033       0  \n",
       "5171      16  "
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_vdc[df_vdc['PRODUTO']== 74043].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final = df_final.fillna(0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Ciclo_x', 'Região', 'Canal', 'Código', 'Descrição_x', 'IAF',\n",
       "       'Tipo de pedido', 'Foco', 'Unidade de negócio', 'Marca', 'Categoria',\n",
       "       'Tipo de promoção', 'Catálogo', 'Tipo de produto', 'Ação consumidor',\n",
       "       'Percentual de desconto consumidor', 'Ação revendedor',\n",
       "       'Percentual de desconto revendedor', 'MATCH', 'PDV', 'CANAL',\n",
       "       'DESCRIÇÃO PDV', 'UF', 'ANALISTA', 'SUPERVISOR', 'SKU', 'curva',\n",
       "       'categoria', 'proj_mar', 'proj_mar+1', 'ESTOQUE ATUAL',\n",
       "       'ESTOQUE EM TRANSITO', 'PEDIDO PENDENTE', 'DDV PREVISTO', 'preço',\n",
       "       'SKU_FINAL', 'C202412', 'C202413', 'C202414', 'C202415', 'C202416',\n",
       "       'C202417', 'C202501', 'C202502', 'C202503', 'C202504', 'C202505',\n",
       "       'C202506', 'C202507', 'C202508', 'C202509', 'C202510', 'C202511',\n",
       "       'C202512', 'Ciclo_y', 'INICIO CICLO', 'FIM CICLO', 'DURAÇÃO',\n",
       "       'dias_ate_inicio', 'Descrição_y', 'chave sku2', 'chave sku1',\n",
       "       'PRECO DE COMPRA', 'PRECO DE VENDA', 'UFPRODUTO', 'PDV GINSENG',\n",
       "       'PRODUTO', '202401', '202402', '202403', '202404', '202405', '202406',\n",
       "       '202407', '202408', '202409', '202410', '202411', '202412', '202413',\n",
       "       '202414', '202415', '202416'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['202401', '202402', '202403', '202404', '202405', '202406', '202407',\n",
       "       '202408', '202409', '202410', '202411', '202412', '202413', '202414',\n",
       "       '202415', '202416'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns[-16:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final.to_clipboard()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "# identificar colunas de ciclo puro (ex: '202407')\n",
    "colunas_ciclo_puro = [col for col in df_final.columns[-16:]]\n",
    "\n",
    "for col in colunas_ciclo_puro:\n",
    "    col_hist = f'C{col}'\n",
    "    if col_hist in df_final.columns:\n",
    "        # mantém o maior valor entre a coluna histórica e a pura\n",
    "        df_final[col_hist] = df_final[[col_hist, col]].max(axis=1)\n",
    "\n",
    "# remove as colunas puras\n",
    "if colunas_ciclo_puro:\n",
    "    df_final.drop(columns=colunas_ciclo_puro, inplace=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Ciclo_x', 'Região', 'Canal', 'Código', 'Descrição_x', 'IAF',\n",
       "       'Tipo de pedido', 'Foco', 'Unidade de negócio', 'Marca', 'Categoria',\n",
       "       'Tipo de promoção', 'Catálogo', 'Tipo de produto', 'Ação consumidor',\n",
       "       'Percentual de desconto consumidor', 'Ação revendedor',\n",
       "       'Percentual de desconto revendedor', 'MATCH', 'PDV', 'CANAL',\n",
       "       'DESCRIÇÃO PDV', 'UF', 'ANALISTA', 'SUPERVISOR', 'SKU', 'curva',\n",
       "       'categoria', 'proj_mar', 'proj_mar+1', 'ESTOQUE ATUAL',\n",
       "       'ESTOQUE EM TRANSITO', 'PEDIDO PENDENTE', 'DDV PREVISTO', 'preço',\n",
       "       'SKU_FINAL', 'C202412', 'C202413', 'C202414', 'C202415', 'C202416',\n",
       "       'C202417', 'C202501', 'C202502', 'C202503', 'C202504', 'C202505',\n",
       "       'C202506', 'C202507', 'C202508', 'C202509', 'C202510', 'C202511',\n",
       "       'C202512', 'Ciclo_y', 'INICIO CICLO', 'FIM CICLO', 'DURAÇÃO',\n",
       "       'dias_ate_inicio', 'Descrição_y', 'chave sku2', 'chave sku1',\n",
       "       'PRECO DE COMPRA', 'PRECO DE VENDA', 'UFPRODUTO', 'PDV GINSENG',\n",
       "       'PRODUTO'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>DATA_VENDA</th>\n",
       "      <th>PDV</th>\n",
       "      <th>pdvs cp</th>\n",
       "      <th>DESC PDV</th>\n",
       "      <th>Código</th>\n",
       "      <th>Descrição</th>\n",
       "      <th>Quantidade</th>\n",
       "      <th>Faturamento</th>\n",
       "      <th>Ciclo</th>\n",
       "      <th>Date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2025-04-01</td>\n",
       "      <td>12993</td>\n",
       "      <td>24253</td>\n",
       "      <td>PERFUMARIA E COSMETICOS QUEIROZ DOURADO LTDA</td>\n",
       "      <td>1004</td>\n",
       "      <td>FLORATTA DES COL MY BLUE 75ml</td>\n",
       "      <td>1</td>\n",
       "      <td>98,99</td>\n",
       "      <td>C202505</td>\n",
       "      <td>2025-04-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2025-04-01</td>\n",
       "      <td>12993</td>\n",
       "      <td>24253</td>\n",
       "      <td>PERFUMARIA E COSMETICOS QUEIROZ DOURADO LTDA</td>\n",
       "      <td>4539</td>\n",
       "      <td>BOTIK CR FAC FIRMADOR AC/HIAL 40g V2</td>\n",
       "      <td>1</td>\n",
       "      <td>129,05</td>\n",
       "      <td>C202505</td>\n",
       "      <td>2025-04-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2025-04-01</td>\n",
       "      <td>12993</td>\n",
       "      <td>24253</td>\n",
       "      <td>PERFUMARIA E COSMETICOS QUEIROZ DOURADO LTDA</td>\n",
       "      <td>29046</td>\n",
       "      <td>BOTICOLL CONNEX DES BDY SPR 100ml V6 PCK</td>\n",
       "      <td>1</td>\n",
       "      <td>38,9</td>\n",
       "      <td>C202505</td>\n",
       "      <td>2025-04-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2025-04-01</td>\n",
       "      <td>12993</td>\n",
       "      <td>24253</td>\n",
       "      <td>PERFUMARIA E COSMETICOS QUEIROZ DOURADO LTDA</td>\n",
       "      <td>47154</td>\n",
       "      <td>CBEM DES ROLL S/ALUM 55ml</td>\n",
       "      <td>1</td>\n",
       "      <td>21,98</td>\n",
       "      <td>C202505</td>\n",
       "      <td>2025-04-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2025-04-01</td>\n",
       "      <td>12993</td>\n",
       "      <td>24253</td>\n",
       "      <td>PERFUMARIA E COSMETICOS QUEIROZ DOURADO LTDA</td>\n",
       "      <td>47411</td>\n",
       "      <td>ARBO NECESS LONA</td>\n",
       "      <td>1</td>\n",
       "      <td>57,26</td>\n",
       "      <td>C202505</td>\n",
       "      <td>2025-04-01</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  DATA_VENDA    PDV  pdvs cp                                       DESC PDV  \\\n",
       "0 2025-04-01  12993    24253   PERFUMARIA E COSMETICOS QUEIROZ DOURADO LTDA   \n",
       "1 2025-04-01  12993    24253   PERFUMARIA E COSMETICOS QUEIROZ DOURADO LTDA   \n",
       "2 2025-04-01  12993    24253   PERFUMARIA E COSMETICOS QUEIROZ DOURADO LTDA   \n",
       "3 2025-04-01  12993    24253   PERFUMARIA E COSMETICOS QUEIROZ DOURADO LTDA   \n",
       "4 2025-04-01  12993    24253   PERFUMARIA E COSMETICOS QUEIROZ DOURADO LTDA   \n",
       "\n",
       "   Código                                 Descrição  Quantidade Faturamento  \\\n",
       "0    1004             FLORATTA DES COL MY BLUE 75ml           1       98,99   \n",
       "1    4539      BOTIK CR FAC FIRMADOR AC/HIAL 40g V2           1      129,05   \n",
       "2   29046  BOTICOLL CONNEX DES BDY SPR 100ml V6 PCK           1        38,9   \n",
       "3   47154                 CBEM DES ROLL S/ALUM 55ml           1       21,98   \n",
       "4   47411                          ARBO NECESS LONA           1       57,26   \n",
       "\n",
       "     Ciclo       Date  \n",
       "0  C202505 2025-04-01  \n",
       "1  C202505 2025-04-01  \n",
       "2  C202505 2025-04-01  \n",
       "3  C202505 2025-04-01  \n",
       "4  C202505 2025-04-01  "
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_irece = pd.read_csv(r\"C:\\Users\\joao.herculano\\GRUPO GINSENG\\Assistência Suprimentos - 2025\\DADOS DOURADO\\VENDA\\COMPILADO_VENDAS.csv\", sep=';')\n",
    "\n",
    "df_irece['DATA_VENDA'] = pd.to_datetime(df_irece['DATA_VENDA'], dayfirst=True)\n",
    "\n",
    "df_irece = pd.merge(df_irece,calendario[['Ciclo','Date']],how='left', left_on='DATA_VENDA',right_on='Date')\n",
    "\n",
    "df_irece.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_irece_agrupado = df_irece.groupby(['PDV','Código','Ciclo'])['Quantidade'].sum().reset_index()\n",
    "\n",
    "df_pivotado = df_irece_agrupado.pivot(index=['PDV', 'Código'], columns='Ciclo', values='Quantidade').reset_index()\n",
    "\n",
    "df_pivotado = df_pivotado.fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['C202413', 'C202414', 'C202415', 'C202416', 'C202417', 'C202501',\n",
       "       'C202502', 'C202503', 'C202504', 'C202505', 'C202506', 'C202507',\n",
       "       'C202508', 'C202509', 'C202510', 'C202511', 'C202512', 'Ciclo_y'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns[37:55]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Ciclo_x', 'Região', 'Canal', 'Código', 'Descrição_x', 'IAF',\n",
       "       'Tipo de pedido', 'Foco', 'Unidade de negócio', 'Marca', 'Categoria',\n",
       "       'Tipo de promoção', 'Catálogo', 'Tipo de produto', 'Ação consumidor',\n",
       "       'Percentual de desconto consumidor', 'Ação revendedor',\n",
       "       'Percentual de desconto revendedor', 'MATCH', 'PDV', 'CANAL',\n",
       "       'DESCRIÇÃO PDV', 'UF', 'ANALISTA', 'SUPERVISOR', 'SKU', 'curva',\n",
       "       'categoria', 'proj_mar', 'proj_mar+1', 'ESTOQUE ATUAL',\n",
       "       'ESTOQUE EM TRANSITO', 'PEDIDO PENDENTE', 'DDV PREVISTO', 'preço',\n",
       "       'SKU_FINAL', '202412', '202413', '202414', '202415', '202416', '202417',\n",
       "       '202501', '202502', '202503', '202504', '202505', '202506', '202507',\n",
       "       '202508', '202509', '202510', '202511', '202512', 'Ciclo_y',\n",
       "       'INICIO CICLO', 'FIM CICLO', 'DURAÇÃO', 'dias_ate_inicio',\n",
       "       'Descrição_y', 'chave sku2', 'chave sku1', 'PRECO DE COMPRA',\n",
       "       'PRECO DE VENDA', 'UFPRODUTO', 'PDV GINSENG', 'PRODUTO'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for col_c in df_final.columns[37:55]:\n",
    "    sufixo = col_c[1:]  # Remove o 'C' do início, ex: 'C202407' -> '202407'\n",
    "    df_final.columns = df_final.columns.str.replace(\"C20\", \"20\", regex=True)\n",
    "\n",
    "\n",
    "df_final.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Ciclo_x</th>\n",
       "      <th>Região</th>\n",
       "      <th>Canal</th>\n",
       "      <th>Código</th>\n",
       "      <th>Descrição_x</th>\n",
       "      <th>IAF</th>\n",
       "      <th>Tipo de pedido</th>\n",
       "      <th>Foco</th>\n",
       "      <th>Unidade de negócio</th>\n",
       "      <th>Marca</th>\n",
       "      <th>...</th>\n",
       "      <th>C202415</th>\n",
       "      <th>C202416</th>\n",
       "      <th>C202417</th>\n",
       "      <th>C202501</th>\n",
       "      <th>C202502</th>\n",
       "      <th>C202503</th>\n",
       "      <th>C202504</th>\n",
       "      <th>C202505</th>\n",
       "      <th>C202506</th>\n",
       "      <th>C202507</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ARBO</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ARBO</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ARBO</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ARBO</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ARBO</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 91 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Ciclo_x Região Canal  Código                Descrição_x  IAF  \\\n",
       "0   202515    NNE  Loja   50052  ARBO LOC DES HID CPO 75ml  Não   \n",
       "1   202515    NNE  Loja   50052  ARBO LOC DES HID CPO 75ml  Não   \n",
       "2   202515    NNE  Loja   50052  ARBO LOC DES HID CPO 75ml  Não   \n",
       "3   202515    NNE  Loja   50052  ARBO LOC DES HID CPO 75ml  Não   \n",
       "4   202515    NNE  Loja   50052  ARBO LOC DES HID CPO 75ml  Não   \n",
       "\n",
       "  Tipo de pedido Foco Unidade de negócio Marca  ... C202415 C202416 C202417  \\\n",
       "0        Semanal  Não                BOT  ARBO  ...     0.0     0.0     0.0   \n",
       "1        Semanal  Não                BOT  ARBO  ...     0.0     0.0     0.0   \n",
       "2        Semanal  Não                BOT  ARBO  ...     0.0     0.0     0.0   \n",
       "3        Semanal  Não                BOT  ARBO  ...     0.0     0.0     0.0   \n",
       "4        Semanal  Não                BOT  ARBO  ...     0.0     0.0     0.0   \n",
       "\n",
       "  C202501 C202502 C202503 C202504 C202505  C202506  C202507  \n",
       "0     0.0     0.0     0.0     0.0     0.0      0.0      0.0  \n",
       "1     0.0     0.0     0.0     0.0     0.0      0.0      0.0  \n",
       "2     0.0     0.0     0.0     0.0     0.0      0.0      0.0  \n",
       "3     0.0     0.0     0.0     0.0     0.0      0.0      0.0  \n",
       "4     0.0     0.0     0.0     0.0     0.0      0.0      0.0  \n",
       "\n",
       "[5 rows x 91 columns]"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final = pd.merge(df_final,df_pivotado,how='left',on=['PDV','Código'])\n",
    "\n",
    "df_final = df_final.fillna(0)\n",
    "\n",
    "df_final.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Ciclo_x', 'Região', 'Canal', 'Código', 'Descrição_x', 'IAF',\n",
       "       'Tipo de pedido', 'Foco', 'Unidade de negócio', 'Marca', 'Categoria',\n",
       "       'Tipo de promoção', 'Catálogo', 'Tipo de produto', 'Ação consumidor',\n",
       "       'Percentual de desconto consumidor', 'Ação revendedor',\n",
       "       'Percentual de desconto revendedor', 'MATCH', 'PDV', 'CANAL',\n",
       "       'DESCRIÇÃO PDV', 'UF', 'ANALISTA', 'SUPERVISOR', 'SKU', 'curva',\n",
       "       'categoria', 'proj_mar', 'proj_mar+1', 'ESTOQUE ATUAL',\n",
       "       'ESTOQUE EM TRANSITO', 'PEDIDO PENDENTE', 'DDV PREVISTO', 'preço',\n",
       "       'SKU_FINAL', '202412', '202413', '202414', '202415', '202416', '202417',\n",
       "       '202501', '202502', '202503', '202504', '202505', '202506', '202507',\n",
       "       '202508', '202509', '202510', '202511', '202512', 'Ciclo_y',\n",
       "       'INICIO CICLO', 'FIM CICLO', 'DURAÇÃO', 'dias_ate_inicio',\n",
       "       'Descrição_y', 'chave sku2', 'chave sku1', 'PRECO DE COMPRA',\n",
       "       'PRECO DE VENDA', 'UFPRODUTO', 'PDV GINSENG', 'PRODUTO', 'C202401',\n",
       "       'C202402', 'C202403', 'C202404', 'C202405', 'C202406', 'C202407',\n",
       "       'C202408', 'C202409', 'C202410', 'C202411', 'C202412', 'C202413',\n",
       "       'C202414', 'C202415', 'C202416', 'C202417', 'C202501', 'C202502',\n",
       "       'C202503', 'C202504', 'C202505', 'C202506', 'C202507'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "# 1. Identificar colunas no formato 'C2024xx', 'C2025xx', etc.\n",
    "colunas_ciclo_c = [col for col in df_pivotado.columns if re.fullmatch(r'C20\\d{4}', col)]\n",
    "\n",
    "for col_c in colunas_ciclo_c:\n",
    "    sufixo = col_c[1:]  # Remove o 'C' do início, ex: 'C202407' -> '202407'\n",
    "    col_hist = col_c\n",
    "    \n",
    "    if col_hist in df_final.columns:\n",
    "        # Pega o maior valor entre a coluna de histórico e a de ciclo com \"C\"\n",
    "        df_final[col_hist] = df_final[[col_hist, col_c]].max(axis=1)\n",
    "\n",
    "# (Opcional) Remover as colunas 'C20xxxx' após o merge\n",
    "df_final.drop(columns=colunas_ciclo_c, inplace=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Ciclo_x', 'Região', 'Canal', 'Código', 'Descrição_x', 'IAF',\n",
       "       'Tipo de pedido', 'Foco', 'Unidade de negócio', 'Marca', 'Categoria',\n",
       "       'Tipo de promoção', 'Catálogo', 'Tipo de produto', 'Ação consumidor',\n",
       "       'Percentual de desconto consumidor', 'Ação revendedor',\n",
       "       'Percentual de desconto revendedor', 'MATCH', 'PDV', 'CANAL',\n",
       "       'DESCRIÇÃO PDV', 'UF', 'ANALISTA', 'SUPERVISOR', 'SKU', 'curva',\n",
       "       'categoria', 'proj_mar', 'proj_mar+1', 'ESTOQUE ATUAL',\n",
       "       'ESTOQUE EM TRANSITO', 'PEDIDO PENDENTE', 'DDV PREVISTO', 'preço',\n",
       "       'SKU_FINAL', '202412', '202413', '202414', '202415', '202416', '202417',\n",
       "       '202501', '202502', '202503', '202504', '202505', '202506', '202507',\n",
       "       '202508', '202509', '202510', '202511', '202512', 'Ciclo_y',\n",
       "       'INICIO CICLO', 'FIM CICLO', 'DURAÇÃO', 'dias_ate_inicio',\n",
       "       'Descrição_y', 'chave sku2', 'chave sku1', 'PRECO DE COMPRA',\n",
       "       'PRECO DE VENDA', 'UFPRODUTO', 'PDV GINSENG', 'PRODUTO'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "202401 C202401\n",
      "202402 C202402\n",
      "202403 C202403\n",
      "202404 C202404\n",
      "202405 C202405\n",
      "202406 C202406\n",
      "202407 C202407\n",
      "202408 C202408\n",
      "202409 C202409\n",
      "202410 C202410\n",
      "202411 C202411\n",
      "202412 C202412\n",
      "202413 C202413\n",
      "202414 C202414\n",
      "202415 C202415\n",
      "202416 C202416\n",
      "202417 C202417\n",
      "202501 C202501\n",
      "202502 C202502\n",
      "202503 C202503\n",
      "202504 C202504\n",
      "202505 C202505\n",
      "202506 C202506\n",
      "202507 C202507\n"
     ]
    }
   ],
   "source": [
    "colunas_ciclo_c = [col for col in df_pivotado.columns if re.fullmatch(r'C20\\d{4}', col)]\n",
    "\n",
    "for col_c in colunas_ciclo_c:\n",
    "    sufixo = col_c[1:]  # Remove o 'C' do início, ex: 'C202407' -> '202407'\n",
    "    col_hist = col_c\n",
    "    print(sufixo,col_c)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final = df_final.drop(columns=['Ciclo_y'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Ciclo_x', 'Região', 'Canal', 'Código', 'Descrição_x', 'IAF',\n",
       "       'Tipo de pedido', 'Foco', 'Unidade de negócio', 'Marca', 'Categoria',\n",
       "       'Tipo de promoção', 'Catálogo', 'Tipo de produto', 'Ação consumidor',\n",
       "       'Percentual de desconto consumidor', 'Ação revendedor',\n",
       "       'Percentual de desconto revendedor', 'MATCH', 'PDV', 'CANAL',\n",
       "       'DESCRIÇÃO PDV', 'UF', 'ANALISTA', 'SUPERVISOR', 'SKU', 'curva',\n",
       "       'categoria', 'proj_mar', 'proj_mar+1', 'ESTOQUE ATUAL',\n",
       "       'ESTOQUE EM TRANSITO', 'PEDIDO PENDENTE', 'DDV PREVISTO', 'preço',\n",
       "       'SKU_FINAL', '202412', '202413', '202414', '202415', '202416', '202417',\n",
       "       '202501', '202502', '202503', '202504', '202505', '202506', '202507',\n",
       "       '202508', '202509', '202510', '202511', '202512', 'INICIO CICLO',\n",
       "       'FIM CICLO', 'DURAÇÃO', 'dias_ate_inicio', 'Descrição_y', 'chave sku2',\n",
       "       'chave sku1', 'PRECO DE COMPRA', 'PRECO DE VENDA', 'UFPRODUTO',\n",
       "       'PDV GINSENG', 'PRODUTO'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(34265, 66)"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final = df_final.drop_duplicates()\n",
    "df_final.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final['chave'] = df_final['PDV'].astype('str') + df_final['Código'].astype('str')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final = df_final.fillna(0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['202412', '202413', '202414', '202415', '202416', '202417', '202501',\n",
       "       '202502', '202503', '202504', '202505', '202506', '202507', '202508',\n",
       "       '202509', '202510', '202511', '202512'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns[36:54]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Ciclo_x', 'Região', 'Canal', 'Código', 'Descrição_x', 'IAF',\n",
       "       'Tipo de pedido', 'Foco', 'Unidade de negócio', 'Marca', 'Categoria',\n",
       "       'Tipo de promoção', 'Catálogo', 'Tipo de produto', 'Ação consumidor',\n",
       "       'Percentual de desconto consumidor', 'Ação revendedor',\n",
       "       'Percentual de desconto revendedor', 'MATCH', 'PDV', 'CANAL',\n",
       "       'DESCRIÇÃO PDV', 'UF', 'ANALISTA', 'SUPERVISOR', 'SKU', 'curva',\n",
       "       'categoria', 'proj_mar', 'proj_mar+1', 'ESTOQUE ATUAL',\n",
       "       'ESTOQUE EM TRANSITO', 'PEDIDO PENDENTE', 'DDV PREVISTO', 'preço',\n",
       "       'SKU_FINAL', '202412', '202413', '202414', '202415', '202416', '202417',\n",
       "       '202501', '202502', '202503', '202504', '202505', '202506', '202507',\n",
       "       '202508', '202509', '202510', '202511', '202512', 'INICIO CICLO',\n",
       "       'FIM CICLO', 'DURAÇÃO', 'dias_ate_inicio', 'Descrição_y', 'chave sku2',\n",
       "       'chave sku1', 'PRECO DE COMPRA', 'PRECO DE VENDA', 'UFPRODUTO',\n",
       "       'PDV GINSENG', 'PRODUTO', 'chave'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define list of target columns\n",
    "sales_2024_cols = df_final.columns[36:54]\n",
    "# Create a new column with the row-wise max\n",
    "df_final['PICO DE VENDAS 2024'] = df_final[sales_2024_cols].max(axis=1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [],
   "source": [
    "vendas_6_meses = df_final.columns[48:54]\n",
    "\n",
    "df_final['Pico Vendas Ultimos 6 ciclos'] = df_final[vendas_6_meses].max(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['202413', '202414', '202415', '202416', '202417', '202501', '202502'], dtype='object')"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns[37:44]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Ciclo_x', 'Região', 'Canal', 'Código', 'Descrição_x', 'IAF',\n",
       "       'Tipo de pedido', 'Foco', 'Unidade de negócio', 'Marca', 'Categoria',\n",
       "       'Tipo de promoção', 'Catálogo', 'Tipo de produto', 'Ação consumidor',\n",
       "       'Percentual de desconto consumidor', 'Ação revendedor',\n",
       "       'Percentual de desconto revendedor', 'MATCH', 'PDV', 'CANAL',\n",
       "       'DESCRIÇÃO PDV', 'UF', 'ANALISTA', 'SUPERVISOR', 'SKU', 'curva',\n",
       "       'categoria', 'proj_mar', 'proj_mar+1', 'ESTOQUE ATUAL',\n",
       "       'ESTOQUE EM TRANSITO', 'PEDIDO PENDENTE', 'DDV PREVISTO', 'preço',\n",
       "       'SKU_FINAL', '202412', '202413', '202414', '202415', '202416', '202417',\n",
       "       '202501', '202502', '202503', '202504', '202505', '202506', '202507',\n",
       "       '202508', '202509', '202510', '202511', '202512', 'INICIO CICLO',\n",
       "       'FIM CICLO', 'DURAÇÃO', 'dias_ate_inicio', 'Descrição_y', 'chave sku2',\n",
       "       'chave sku1', 'PRECO DE COMPRA', 'PRECO DE VENDA', 'UFPRODUTO',\n",
       "       'PDV GINSENG', 'PRODUTO', 'chave', 'PICO DE VENDAS 2024',\n",
       "       'Pico Vendas Ultimos 6 ciclos'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Ciclo_x', 'Região', 'Canal', 'Código', 'Descrição_x', 'IAF',\n",
       "       'Tipo de pedido', 'Foco', 'Unidade de negócio', 'Marca', 'Categoria',\n",
       "       'Tipo de promoção', 'Catálogo', 'Tipo de produto', 'Ação consumidor',\n",
       "       'Percentual de desconto consumidor', 'Ação revendedor',\n",
       "       'Percentual de desconto revendedor', 'MATCH', 'PDV', 'CANAL',\n",
       "       'DESCRIÇÃO PDV', 'UF', 'ANALISTA', 'SUPERVISOR', 'SKU', 'curva',\n",
       "       'categoria', 'proj_mar', 'proj_mar+1', 'ESTOQUE ATUAL',\n",
       "       'ESTOQUE EM TRANSITO', 'PEDIDO PENDENTE', 'DDV PREVISTO', 'preço',\n",
       "       'SKU_FINAL', '202412', '202413', '202414', '202415', '202416', '202417',\n",
       "       '202501', '202502', '202503', '202504', '202505', '202506', '202507',\n",
       "       '202508', '202509', '202510', '202511', '202512', 'INICIO CICLO',\n",
       "       'FIM CICLO', 'DURAÇÃO', 'dias_ate_inicio', 'Descrição_y', 'chave sku2',\n",
       "       'chave sku1', 'PRECO DE COMPRA', 'PRECO DE VENDA', 'UFPRODUTO',\n",
       "       'PDV GINSENG', 'PRODUTO', 'chave', 'PICO DE VENDAS 2024',\n",
       "       'Pico Vendas Ultimos 6 ciclos'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['202412', '202413', '202414', '202415', '202416', '202417', '202501',\n",
       "       '202502', '202503', '202504', '202505', '202506', '202507', '202508',\n",
       "       '202509', '202510', '202511', '202512'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns[36:54]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Ciclo_x</th>\n",
       "      <th>Região</th>\n",
       "      <th>Canal</th>\n",
       "      <th>Código</th>\n",
       "      <th>Descrição_x</th>\n",
       "      <th>IAF</th>\n",
       "      <th>Tipo de pedido</th>\n",
       "      <th>Foco</th>\n",
       "      <th>Unidade de negócio</th>\n",
       "      <th>Marca</th>\n",
       "      <th>...</th>\n",
       "      <th>chave sku2</th>\n",
       "      <th>chave sku1</th>\n",
       "      <th>PRECO DE COMPRA</th>\n",
       "      <th>PRECO DE VENDA</th>\n",
       "      <th>UFPRODUTO</th>\n",
       "      <th>PDV GINSENG</th>\n",
       "      <th>PRODUTO</th>\n",
       "      <th>chave</th>\n",
       "      <th>PICO DE VENDAS 2024</th>\n",
       "      <th>Pico Vendas Ultimos 6 ciclos</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ARBO</td>\n",
       "      <td>...</td>\n",
       "      <td>AL50052</td>\n",
       "      <td>AL50052</td>\n",
       "      <td>11.72</td>\n",
       "      <td>37.9</td>\n",
       "      <td>AL50052</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1252250052</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ARBO</td>\n",
       "      <td>...</td>\n",
       "      <td>AL50052</td>\n",
       "      <td>AL50052</td>\n",
       "      <td>11.72</td>\n",
       "      <td>37.9</td>\n",
       "      <td>AL50052</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1281750052</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ARBO</td>\n",
       "      <td>...</td>\n",
       "      <td>AL50052</td>\n",
       "      <td>AL50052</td>\n",
       "      <td>11.72</td>\n",
       "      <td>37.9</td>\n",
       "      <td>AL50052</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1281850052</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ARBO</td>\n",
       "      <td>...</td>\n",
       "      <td>AL50052</td>\n",
       "      <td>AL50052</td>\n",
       "      <td>11.72</td>\n",
       "      <td>37.9</td>\n",
       "      <td>AL50052</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1282050052</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ARBO</td>\n",
       "      <td>...</td>\n",
       "      <td>AL50052</td>\n",
       "      <td>AL50052</td>\n",
       "      <td>11.72</td>\n",
       "      <td>37.9</td>\n",
       "      <td>AL50052</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1282350052</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 69 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Ciclo_x Região Canal  Código                Descrição_x  IAF  \\\n",
       "0   202515    NNE  Loja   50052  ARBO LOC DES HID CPO 75ml  Não   \n",
       "1   202515    NNE  Loja   50052  ARBO LOC DES HID CPO 75ml  Não   \n",
       "2   202515    NNE  Loja   50052  ARBO LOC DES HID CPO 75ml  Não   \n",
       "3   202515    NNE  Loja   50052  ARBO LOC DES HID CPO 75ml  Não   \n",
       "4   202515    NNE  Loja   50052  ARBO LOC DES HID CPO 75ml  Não   \n",
       "\n",
       "  Tipo de pedido Foco Unidade de negócio Marca  ... chave sku2 chave sku1  \\\n",
       "0        Semanal  Não                BOT  ARBO  ...    AL50052    AL50052   \n",
       "1        Semanal  Não                BOT  ARBO  ...    AL50052    AL50052   \n",
       "2        Semanal  Não                BOT  ARBO  ...    AL50052    AL50052   \n",
       "3        Semanal  Não                BOT  ARBO  ...    AL50052    AL50052   \n",
       "4        Semanal  Não                BOT  ARBO  ...    AL50052    AL50052   \n",
       "\n",
       "  PRECO DE COMPRA PRECO DE VENDA UFPRODUTO PDV GINSENG PRODUTO       chave  \\\n",
       "0           11.72           37.9   AL50052         0.0       0  1252250052   \n",
       "1           11.72           37.9   AL50052         0.0       0  1281750052   \n",
       "2           11.72           37.9   AL50052         0.0       0  1281850052   \n",
       "3           11.72           37.9   AL50052         0.0       0  1282050052   \n",
       "4           11.72           37.9   AL50052         0.0       0  1282350052   \n",
       "\n",
       "   PICO DE VENDAS 2024  Pico Vendas Ultimos 6 ciclos  \n",
       "0                  2.0                           0.0  \n",
       "1                  5.0                           0.0  \n",
       "2                  1.0                           1.0  \n",
       "3                  1.0                           0.0  \n",
       "4                  1.0                           0.0  \n",
       "\n",
       "[5 rows x 69 columns]"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\joao.herculano\\AppData\\Local\\Temp\\ipykernel_2580\\3082173574.py:27: DeprecationWarning: DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation. Either pass `include_groups=False` to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning.\n",
      "  .apply(calcular_crescimento)   # retorna índice com PDV\n"
     ]
    }
   ],
   "source": [
    "# Define as colunas mensais\n",
    "colunas_mensais = df_final.columns[36:54]\n",
    "\n",
    "# Função de cálculo por grupo\n",
    "def calcular_crescimento(grupo):\n",
    "    soma_mensal = grupo[colunas_mensais].sum()\n",
    "    variacao_mensal = soma_mensal.pct_change().dropna()\n",
    "    variacao_mensal = variacao_mensal[np.isfinite(variacao_mensal)]\n",
    "\n",
    "    if len(variacao_mensal) == 0:\n",
    "        return pd.Series({'CRESCIMENTO': np.nan})\n",
    "\n",
    "    media = variacao_mensal.mean()\n",
    "    desvio = variacao_mensal.std()\n",
    "\n",
    "    limite_sup = media + 2 * desvio\n",
    "    limite_inf = media - 2 * desvio\n",
    "\n",
    "    variacoes_filtradas = variacao_mensal[variacao_mensal.between(limite_inf, limite_sup)]\n",
    "    crescimento = round(variacoes_filtradas.mean(), 4)\n",
    "    return pd.Series({'CRESCIMENTO': crescimento})\n",
    "\n",
    "# Aplica a função por PDV\n",
    "crescimento_por_pdv = (\n",
    "    df_final\n",
    "    .groupby('PDV')\n",
    "    .apply(calcular_crescimento)   # retorna índice com PDV\n",
    "    .reset_index(level=0)          # reseta só o nível PDV\n",
    ")\n",
    "\n",
    "# Merge do resultado de volta no dataframe original\n",
    "df_final = df_final.merge(crescimento_por_pdv, on='PDV', how='left')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0857)"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Suponha que os meses estão nas colunas 10 a 26 (17 colunas = 17 meses)\n",
    "colunas_mensais = df_final.columns[36:54]\n",
    "\n",
    "# Passo 1: Soma todas as linhas (itens) por mês → resultado: total por mês\n",
    "soma_mensal = df_final[colunas_mensais].sum()\n",
    "\n",
    "# Passo 2: Calcula a variação percentual de um mês para o outro\n",
    "variacao_mensal = soma_mensal.pct_change()\n",
    "variacao_mensal = variacao_mensal.dropna()\n",
    "\n",
    "variacao_mensal = variacao_mensal[np.isfinite(variacao_mensal)]\n",
    "\n",
    "# Passo 3: Calcula a média da variação (ignorando o primeiro NaN)\n",
    "media_variacao = variacao_mensal[1:].mean()\n",
    "\n",
    "# Calcula média e desvio padrão\n",
    "media = variacao_mensal.mean()\n",
    "desvio = variacao_mensal.std()\n",
    "\n",
    "# Define limite (ex: 2 desvios padrão)\n",
    "limite_superior = media + 2 * desvio\n",
    "limite_inferior = media - 2 * desvio\n",
    "\n",
    "# Filtra dados dentro do limite\n",
    "filtro = variacao_mensal.between(limite_inferior, limite_superior)\n",
    "df_filtrado = variacao_mensal[filtro]\n",
    "CRESCIMENTO = round(df_filtrado.mean(),4)\n",
    "\n",
    "df_final['CRESCIMENTO_GERAL'] = 0.2\n",
    "\n",
    "CRESCIMENTO\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['202412', '202413', '202414', '202415', '202416', '202417', '202501',\n",
       "       '202502', '202503', '202504', '202505', '202506', '202507', '202508',\n",
       "       '202509', '202510', '202511', '202512'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns[36:54]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CANAL</th>\n",
       "      <th>UF</th>\n",
       "      <th>Código</th>\n",
       "      <th>med_por_canal</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>TODOS</td>\n",
       "      <td>AL</td>\n",
       "      <td>1428</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>TODOS</td>\n",
       "      <td>AL</td>\n",
       "      <td>1634</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>TODOS</td>\n",
       "      <td>AL</td>\n",
       "      <td>1640</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>TODOS</td>\n",
       "      <td>AL</td>\n",
       "      <td>1650</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>TODOS</td>\n",
       "      <td>AL</td>\n",
       "      <td>1806</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4445</th>\n",
       "      <td>VD</td>\n",
       "      <td>VDC</td>\n",
       "      <td>88471</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4446</th>\n",
       "      <td>VD</td>\n",
       "      <td>VDC</td>\n",
       "      <td>88563</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4447</th>\n",
       "      <td>VD</td>\n",
       "      <td>VDC</td>\n",
       "      <td>88730</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4448</th>\n",
       "      <td>VD</td>\n",
       "      <td>VDC</td>\n",
       "      <td>89421</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4449</th>\n",
       "      <td>VD</td>\n",
       "      <td>VDC</td>\n",
       "      <td>89495</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4450 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      CANAL   UF  Código  med_por_canal\n",
       "0     TODOS   AL    1428            0.0\n",
       "1     TODOS   AL    1634            0.0\n",
       "2     TODOS   AL    1640            0.0\n",
       "3     TODOS   AL    1650            0.0\n",
       "4     TODOS   AL    1806            0.0\n",
       "...     ...  ...     ...            ...\n",
       "4445     VD  VDC   88471            0.0\n",
       "4446     VD  VDC   88563            0.0\n",
       "4447     VD  VDC   88730            0.0\n",
       "4448     VD  VDC   89421            0.0\n",
       "4449     VD  VDC   89495            0.0\n",
       "\n",
       "[4450 rows x 4 columns]"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vendas_todos_historicos = df_final.columns[36:54]\n",
    "\n",
    "df_final['MEDIANA DO HISTÓRICO'] = df_final[vendas_todos_historicos].median(axis=1)\n",
    "\n",
    "df_final['MEDIA DO HISTÓRICO'] = df_final[vendas_todos_historicos].mean(axis=1)\n",
    "\n",
    "medi = df_final.groupby(['CANAL','UF','Código'])['MEDIANA DO HISTÓRICO'].max().reset_index()\n",
    "medi = medi.rename(columns={'MEDIANA DO HISTÓRICO':'med_por_canal'})\n",
    "\n",
    "df_final = pd.merge(left=df_final, right=medi,on=['CANAL','UF','Código'],how='left')\n",
    "\n",
    "medi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'202415'"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns[39]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [],
   "source": [
    "mesmo_ciclo_ano_passado = df_final.columns[39]\n",
    "ciclo_ano_passado = df_final.columns[39]\n",
    "df_final[ciclo_ano_passado] = df_final[mesmo_ciclo_ano_passado]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"df_final['CRESCIMENTO_GERAL'] = 0.2\\ndf_final['CRESCIMENTO'] = 0.2\""
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''df_final['CRESCIMENTO_GERAL'] = 0.2\n",
    "df_final['CRESCIMENTO'] = 0.2'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(34265, 77)"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final['CRESCIMENTO_FINAL'] = df_final['CRESCIMENTO_GERAL'] + df_final['CRESCIMENTO'] #crescimento do pdv\n",
    "\n",
    "df_final['CRESCIMENTO_FINAL'] = np.where(df_final['CRESCIMENTO_GERAL'] + df_final['CRESCIMENTO']>0.8,0.8,df_final['CRESCIMENTO_GERAL'] + df_final['CRESCIMENTO'])\n",
    "\n",
    "df_final['CRESCIMENTO_FINAL'] = np.where(df_final['CRESCIMENTO_GERAL'] + df_final['CRESCIMENTO']<0,0,df_final['CRESCIMENTO_GERAL'] + df_final['CRESCIMENTO'])\n",
    "\n",
    "df_final['MEDIANA DO HISTÓRICO2'] = np.where(df_final['MEDIANA DO HISTÓRICO']==0,df_final['MEDIA DO HISTÓRICO'] ,df_final['MEDIANA DO HISTÓRICO'])\n",
    "\n",
    "# Primeiro cálculo intermediário\n",
    "df_final['PV GINSENG'] = np.where(df_final['CRESCIMENTO_FINAL'] * df_final[ciclo_ano_passado] + df_final[ciclo_ano_passado] <1,\n",
    "                                    round(df_final['CRESCIMENTO_FINAL'] * df_final['MEDIANA DO HISTÓRICO2']+ df_final['MEDIANA DO HISTÓRICO2'],0), \n",
    "                                    round(df_final['CRESCIMENTO_FINAL']*df_final[ciclo_ano_passado]+df_final[ciclo_ano_passado],0))\n",
    "\n",
    "\n",
    "df_final.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PDV\n",
       "12522    445\n",
       "12817    445\n",
       "12818    445\n",
       "12820    445\n",
       "12823    445\n",
       "        ... \n",
       "24257    445\n",
       "24268    445\n",
       "24269    445\n",
       "24293    445\n",
       "23813    445\n",
       "Name: count, Length: 77, dtype: Int64"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final['PDV'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Ciclo_x</th>\n",
       "      <th>Região</th>\n",
       "      <th>Canal</th>\n",
       "      <th>Código</th>\n",
       "      <th>Descrição_x</th>\n",
       "      <th>IAF</th>\n",
       "      <th>Tipo de pedido</th>\n",
       "      <th>Foco</th>\n",
       "      <th>Unidade de negócio</th>\n",
       "      <th>Marca</th>\n",
       "      <th>...</th>\n",
       "      <th>PICO DE VENDAS 2024</th>\n",
       "      <th>Pico Vendas Ultimos 6 ciclos</th>\n",
       "      <th>CRESCIMENTO</th>\n",
       "      <th>CRESCIMENTO_GERAL</th>\n",
       "      <th>MEDIANA DO HISTÓRICO</th>\n",
       "      <th>MEDIA DO HISTÓRICO</th>\n",
       "      <th>med_por_canal</th>\n",
       "      <th>CRESCIMENTO_FINAL</th>\n",
       "      <th>MEDIANA DO HISTÓRICO2</th>\n",
       "      <th>PV GINSENG</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ARBO</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0762</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.2762</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ARBO</td>\n",
       "      <td>...</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0867</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.555556</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.2867</td>\n",
       "      <td>0.555556</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ARBO</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.1065</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3065</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ARBO</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0882</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.2882</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ARBO</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0745</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.2745</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 77 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Ciclo_x Região Canal  Código                Descrição_x  IAF  \\\n",
       "0   202515    NNE  Loja   50052  ARBO LOC DES HID CPO 75ml  Não   \n",
       "1   202515    NNE  Loja   50052  ARBO LOC DES HID CPO 75ml  Não   \n",
       "2   202515    NNE  Loja   50052  ARBO LOC DES HID CPO 75ml  Não   \n",
       "3   202515    NNE  Loja   50052  ARBO LOC DES HID CPO 75ml  Não   \n",
       "4   202515    NNE  Loja   50052  ARBO LOC DES HID CPO 75ml  Não   \n",
       "\n",
       "  Tipo de pedido Foco Unidade de negócio Marca  ... PICO DE VENDAS 2024  \\\n",
       "0        Semanal  Não                BOT  ARBO  ...                 2.0   \n",
       "1        Semanal  Não                BOT  ARBO  ...                 5.0   \n",
       "2        Semanal  Não                BOT  ARBO  ...                 1.0   \n",
       "3        Semanal  Não                BOT  ARBO  ...                 1.0   \n",
       "4        Semanal  Não                BOT  ARBO  ...                 1.0   \n",
       "\n",
       "  Pico Vendas Ultimos 6 ciclos CRESCIMENTO CRESCIMENTO_GERAL  \\\n",
       "0                          0.0      0.0762               0.2   \n",
       "1                          0.0      0.0867               0.2   \n",
       "2                          1.0      0.1065               0.2   \n",
       "3                          0.0      0.0882               0.2   \n",
       "4                          0.0      0.0745               0.2   \n",
       "\n",
       "  MEDIANA DO HISTÓRICO MEDIA DO HISTÓRICO med_por_canal CRESCIMENTO_FINAL  \\\n",
       "0                  0.0           0.111111           0.0            0.2762   \n",
       "1                  0.0           0.555556           0.0            0.2867   \n",
       "2                  0.0           0.055556           0.0            0.3065   \n",
       "3                  0.0           0.055556           0.0            0.2882   \n",
       "4                  0.0           0.111111           0.0            0.2745   \n",
       "\n",
       "   MEDIANA DO HISTÓRICO2  PV GINSENG  \n",
       "0               0.111111         0.0  \n",
       "1               0.555556         1.0  \n",
       "2               0.055556         0.0  \n",
       "3               0.055556         0.0  \n",
       "4               0.111111         0.0  \n",
       "\n",
       "[5 rows x 77 columns]"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final = df_final.drop(columns=['MEDIANA DO HISTÓRICO2'] )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['202414', '202415', '202416', '202417', '202501', '202502', '202503'], dtype='object')"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns[38:45]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['202508', '202509', '202510', '202511', '202512'], dtype='object')"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns[49:54]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final = df_final.rename(columns={df_final.columns[49]: \"C-4\", df_final.columns[50]: \"C-3\",df_final.columns[51]: \"C-2\",df_final.columns[52]: \"C-1\",df_final.columns[53]: \"Atual\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['202416', '202417', '202501', '202502', '202503', '202504', '202505',\n",
       "       '202506', '202507'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns[40:49]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final.drop(columns=df_final.columns[40:49], inplace=True)\n",
    "df_final.drop(columns=df_final.columns[37:39], inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final['COBERTURA ATUAL'] = df_final['ESTOQUE ATUAL'].astype(float) / df_final['DDV PREVISTO'].astype(float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final['COBERTURA ATUAL'] = df_final['COBERTURA ATUAL'].replace([np.inf, -np.inf], 0)\n",
    "\n",
    "df_final['COBERTURA ATUAL'] = round(df_final['COBERTURA ATUAL'],0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "COBERTURA ATUAL\n",
       "0.0       6787\n",
       "100.0      699\n",
       "50.0       447\n",
       "40.0       375\n",
       "20.0       342\n",
       "          ... \n",
       "245.0        1\n",
       "778.0        1\n",
       "811.0        1\n",
       "1967.0       1\n",
       "644.0        1\n",
       "Name: count, Length: 641, dtype: int64"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final['COBERTURA ATUAL'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final[\"DDV PREVISTO\"] = df_final[\"DDV PREVISTO\"].astype(float)\n",
    "\n",
    "df_final[\"DDV PREVISTO\"] = df_final[\"DDV PREVISTO\"].fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PDV\n",
       "12522    445\n",
       "12817    445\n",
       "12818    445\n",
       "12820    445\n",
       "12823    445\n",
       "        ... \n",
       "24257    445\n",
       "24268    445\n",
       "24269    445\n",
       "24293    445\n",
       "23813    445\n",
       "Name: count, Length: 77, dtype: Int64"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final['PDV'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [],
   "source": [
    "r'''colunas_chave = [c for c in df_final.columns if c not in [\"DDV PREVISTO\", \"COBERTURA ATUAL\"]]\n",
    "\n",
    "\n",
    "# encontrar índice da linha com maior DDV e depois maior COBERTURA\n",
    "idx = (\n",
    "df_final\n",
    ".sort_values([\"DDV PREVISTO\", \"COBERTURA ATUAL\"], ascending=[False, False])\n",
    ".groupby(colunas_chave, as_index=False)\n",
    ".head(1)\n",
    ")\n",
    "\n",
    "\n",
    "# reordenar colunas como no original\n",
    "df_final_dedup = idx[df_final.columns.tolist()].reset_index(drop=True)'''\n",
    "\n",
    "df_final_dedup = df_final"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Ciclo_x', 'Região', 'Canal', 'Código', 'Descrição_x', 'IAF',\n",
       "       'Tipo de pedido', 'Foco', 'Unidade de negócio', 'Marca', 'Categoria',\n",
       "       'Tipo de promoção', 'Catálogo', 'Tipo de produto', 'Ação consumidor',\n",
       "       'Percentual de desconto consumidor', 'Ação revendedor',\n",
       "       'Percentual de desconto revendedor', 'MATCH', 'PDV', 'CANAL',\n",
       "       'DESCRIÇÃO PDV', 'UF', 'ANALISTA', 'SUPERVISOR', 'SKU', 'curva',\n",
       "       'categoria', 'proj_mar', 'proj_mar+1', 'ESTOQUE ATUAL',\n",
       "       'ESTOQUE EM TRANSITO', 'PEDIDO PENDENTE', 'DDV PREVISTO', 'preço',\n",
       "       'SKU_FINAL', '202412', '202415', 'C-4', 'C-3', 'C-2', 'C-1', 'Atual',\n",
       "       'INICIO CICLO', 'FIM CICLO', 'DURAÇÃO', 'dias_ate_inicio',\n",
       "       'Descrição_y', 'chave sku2', 'chave sku1', 'PRECO DE COMPRA',\n",
       "       'PRECO DE VENDA', 'UFPRODUTO', 'PDV GINSENG', 'PRODUTO', 'chave',\n",
       "       'PICO DE VENDAS 2024', 'Pico Vendas Ultimos 6 ciclos', 'CRESCIMENTO',\n",
       "       'CRESCIMENTO_GERAL', 'MEDIANA DO HISTÓRICO', 'MEDIA DO HISTÓRICO',\n",
       "       'med_por_canal', 'CRESCIMENTO_FINAL', 'PV GINSENG', 'COBERTURA ATUAL'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Ação consumidor',\n",
       " 'Percentual de desconto consumidor',\n",
       " 'Ação revendedor',\n",
       " 'Percentual de desconto revendedor',\n",
       " '202408')"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Columns to bring up front\n",
    "priority_cols = [\n",
    "    'SKU',\t'Marca',\t'INICIO CICLO',\n",
    "    'FIM CICLO',\t'DURAÇÃO',\t'PRECO DE COMPRA',\t'PRECO DE VENDA',\n",
    "    'UFPRODUTO',\t'Item Desativado',\t'Data Prevista Regularização',\n",
    "    'ANALISTA',\t'UF',\t'CANAL',\t'PDV',\t'DESCRIÇÃO PDV',\t'Classe',\n",
    "    'Descrição',\t'Categoria',\n",
    "\t'ESTOQUE ATUAL', 'ESTOQUE EM TRANSITO',\t'COBERTURA ATUAL',\n",
    "    'Pedido Pendente',\t'PICO DE VENDAS 2024','Pico Vendas Ultimos 6 ciclos',\n",
    "    'C-4',\t'C-3',\t'C-2',\t'C-1',\t'Histórico de Vendas do Ciclo Atual',\n",
    "    'Dias sem venda'\n",
    "]\n",
    "\n",
    "# All remaining columns\n",
    "other_cols = [col for col in df_final_dedup.columns if col not in priority_cols]\n",
    "\n",
    "# Reorder\n",
    "'Ação consumidor', 'Percentual de desconto consumidor', 'Ação revendedor', 'Percentual de desconto revendedor', '202408'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Ciclo_x</th>\n",
       "      <th>Região</th>\n",
       "      <th>Canal</th>\n",
       "      <th>Código</th>\n",
       "      <th>Descrição_x</th>\n",
       "      <th>IAF</th>\n",
       "      <th>Tipo de pedido</th>\n",
       "      <th>Foco</th>\n",
       "      <th>Unidade de negócio</th>\n",
       "      <th>Marca</th>\n",
       "      <th>...</th>\n",
       "      <th>PICO DE VENDAS 2024</th>\n",
       "      <th>Pico Vendas Ultimos 6 ciclos</th>\n",
       "      <th>CRESCIMENTO</th>\n",
       "      <th>CRESCIMENTO_GERAL</th>\n",
       "      <th>MEDIANA DO HISTÓRICO</th>\n",
       "      <th>MEDIA DO HISTÓRICO</th>\n",
       "      <th>med_por_canal</th>\n",
       "      <th>CRESCIMENTO_FINAL</th>\n",
       "      <th>PV GINSENG</th>\n",
       "      <th>COBERTURA ATUAL</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Loja</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ARBO</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>116</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Todos</td>\n",
       "      <td>57640</td>\n",
       "      <td>ARBO NECESSAIRE PES</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ARBO</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>193</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Todos</td>\n",
       "      <td>51803</td>\n",
       "      <td>BOTI BABY COND V2 400ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>BOTI BABY</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>270</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Todos</td>\n",
       "      <td>52023</td>\n",
       "      <td>BOTI BABY LOC HID CPO 400ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>BOTI BABY</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>347</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>Todos</td>\n",
       "      <td>52025</td>\n",
       "      <td>BOTI BABY SAB LIQ GLIC V2 400ml</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>BOTI BABY</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33919</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>VD</td>\n",
       "      <td>36826</td>\n",
       "      <td>REF MAKE B PO COMP FAC HYAL 70 8g</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>MAKE B.</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33996</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>VD</td>\n",
       "      <td>46568</td>\n",
       "      <td>SOPHIE BOLSA NYLON</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>SOPHIE</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34073</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>VD</td>\n",
       "      <td>47949</td>\n",
       "      <td>THE BLEND EDP 100ml V2</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>THE BLEND</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34150</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>VD</td>\n",
       "      <td>49038</td>\n",
       "      <td>ZAAD BALM POS BARB 110g V4</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ZAAD</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34227</th>\n",
       "      <td>202515</td>\n",
       "      <td>NNE</td>\n",
       "      <td>VD</td>\n",
       "      <td>48368</td>\n",
       "      <td>ZAAD ESPUM P/ BARB 200ml V4</td>\n",
       "      <td>Não</td>\n",
       "      <td>Semanal</td>\n",
       "      <td>Não</td>\n",
       "      <td>BOT</td>\n",
       "      <td>ZAAD</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>445 rows × 66 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       Ciclo_x Região  Canal  Código                        Descrição_x  IAF  \\\n",
       "39      202515    NNE   Loja   50052          ARBO LOC DES HID CPO 75ml  Não   \n",
       "116     202515    NNE  Todos   57640                ARBO NECESSAIRE PES  Não   \n",
       "193     202515    NNE  Todos   51803            BOTI BABY COND V2 400ml  Não   \n",
       "270     202515    NNE  Todos   52023        BOTI BABY LOC HID CPO 400ml  Não   \n",
       "347     202515    NNE  Todos   52025    BOTI BABY SAB LIQ GLIC V2 400ml  Não   \n",
       "...        ...    ...    ...     ...                                ...  ...   \n",
       "33919   202515    NNE     VD   36826  REF MAKE B PO COMP FAC HYAL 70 8g  Não   \n",
       "33996   202515    NNE     VD   46568                 SOPHIE BOLSA NYLON  Não   \n",
       "34073   202515    NNE     VD   47949             THE BLEND EDP 100ml V2  Não   \n",
       "34150   202515    NNE     VD   49038         ZAAD BALM POS BARB 110g V4  Não   \n",
       "34227   202515    NNE     VD   48368        ZAAD ESPUM P/ BARB 200ml V4  Não   \n",
       "\n",
       "      Tipo de pedido Foco Unidade de negócio      Marca  ...  \\\n",
       "39           Semanal  Não                BOT       ARBO  ...   \n",
       "116          Semanal  Não                BOT       ARBO  ...   \n",
       "193          Semanal  Não                BOT  BOTI BABY  ...   \n",
       "270          Semanal  Não                BOT  BOTI BABY  ...   \n",
       "347          Semanal  Não                BOT  BOTI BABY  ...   \n",
       "...              ...  ...                ...        ...  ...   \n",
       "33919        Semanal  Não                BOT    MAKE B.  ...   \n",
       "33996        Semanal  Não                BOT     SOPHIE  ...   \n",
       "34073        Semanal  Não                BOT  THE BLEND  ...   \n",
       "34150        Semanal  Não                BOT       ZAAD  ...   \n",
       "34227        Semanal  Não                BOT       ZAAD  ...   \n",
       "\n",
       "      PICO DE VENDAS 2024 Pico Vendas Ultimos 6 ciclos CRESCIMENTO  \\\n",
       "39                    0.0                          0.0         NaN   \n",
       "116                   0.0                          0.0         NaN   \n",
       "193                   0.0                          0.0         NaN   \n",
       "270                   0.0                          0.0         NaN   \n",
       "347                   0.0                          0.0         NaN   \n",
       "...                   ...                          ...         ...   \n",
       "33919                 0.0                          0.0         NaN   \n",
       "33996                 0.0                          0.0         NaN   \n",
       "34073                 0.0                          0.0         NaN   \n",
       "34150                 0.0                          0.0         NaN   \n",
       "34227                 0.0                          0.0         NaN   \n",
       "\n",
       "      CRESCIMENTO_GERAL MEDIANA DO HISTÓRICO MEDIA DO HISTÓRICO med_por_canal  \\\n",
       "39                  0.2                  0.0                0.0           0.0   \n",
       "116                 0.2                  0.0                0.0           0.0   \n",
       "193                 0.2                  0.0                0.0           3.0   \n",
       "270                 0.2                  0.0                0.0           3.0   \n",
       "347                 0.2                  0.0                0.0           8.0   \n",
       "...                 ...                  ...                ...           ...   \n",
       "33919               0.2                  0.0                0.0           0.0   \n",
       "33996               0.2                  0.0                0.0           1.0   \n",
       "34073               0.2                  0.0                0.0          17.0   \n",
       "34150               0.2                  0.0                0.0           0.0   \n",
       "34227               0.2                  0.0                0.0           1.0   \n",
       "\n",
       "      CRESCIMENTO_FINAL  PV GINSENG  COBERTURA ATUAL  \n",
       "39                  NaN         NaN              NaN  \n",
       "116                 NaN         NaN              NaN  \n",
       "193                 NaN         NaN              NaN  \n",
       "270                 NaN         NaN              NaN  \n",
       "347                 NaN         NaN              NaN  \n",
       "...                 ...         ...              ...  \n",
       "33919               NaN         NaN              NaN  \n",
       "33996               NaN         NaN              NaN  \n",
       "34073               NaN         NaN              NaN  \n",
       "34150               NaN         NaN              NaN  \n",
       "34227               NaN         NaN              NaN  \n",
       "\n",
       "[445 rows x 66 columns]"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final_dedup[df_final_dedup['PDV'] == 21007]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final_dedup.drop(columns=['dias_ate_inicio','SKU_FINAL',\n",
    "                             'CRESCIMENTO'],inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final_dedup['INICIO CICLO'] = pd.to_datetime(df_final_dedup['INICIO CICLO'], dayfirst=True).dt.strftime('%d/%m/%Y')\n",
    "\n",
    "df_final_dedup['FIM CICLO'] = pd.to_datetime(df_final_dedup['FIM CICLO'], dayfirst=True).dt.strftime('%d/%m/%Y')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final_dedup['Percentual de desconto revendedor'] = np.where((df_final_dedup['Percentual de desconto revendedor'].isna()) & (~df_final_dedup['Percentual de desconto consumidor'].isna()),df_final_dedup['Percentual de desconto consumidor'],df_final_dedup['Percentual de desconto revendedor'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_pdv_origi['PDV'] = df_pdv_origi['PDV'].astype('Int64')\n",
    "df_final_dedup['PDV'] = df_final_dedup['PDV'].astype('Int64')\n",
    "\n",
    "\n",
    "df_final_dedup = pd.merge(left=df_final_dedup,right=df_pdv_origi[['PDV','CANAL','UF']],how='left',on='PDV')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(0)"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final_dedup['Código'].isna().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_irece_agrupado = df_irece.groupby(['PDV','Código','Ciclo'])['Quantidade'].sum().reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final_dedup = df_final_dedup.rename(columns={'CANAL_x':'CANAL','UF_X':'UF','Marca':'LINHA'})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PDV\n",
       "12522    445\n",
       "12817    445\n",
       "12818    445\n",
       "12820    445\n",
       "12823    445\n",
       "        ... \n",
       "24257    445\n",
       "24268    445\n",
       "24269    445\n",
       "24293    445\n",
       "23813    445\n",
       "Name: count, Length: 77, dtype: Int64"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final_dedup['PDV'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final_dedup = df_final_dedup.rename(columns={'UF_x':'UF','DESCRICAO_y':'DESCRICAO'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final_dedup = df_final_dedup.drop(columns={'CANAL_y', 'UF_y'})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final_dedup['Estoque Total'] = df_final_dedup['ESTOQUE ATUAL'].astype(float) + df_final_dedup['ESTOQUE EM TRANSITO'].astype(float) + df_final_dedup['PEDIDO PENDENTE'].astype(float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'202415'"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ciclo_ano_passado"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Ciclo_x', 'Região', 'Canal', 'Código', 'Descrição_x', 'IAF',\n",
       "       'Tipo de pedido', 'Foco', 'Unidade de negócio', 'LINHA', 'Categoria',\n",
       "       'Tipo de promoção', 'Catálogo', 'Tipo de produto', 'Ação consumidor',\n",
       "       'Percentual de desconto consumidor', 'Ação revendedor',\n",
       "       'Percentual de desconto revendedor', 'MATCH', 'PDV', 'CANAL',\n",
       "       'DESCRIÇÃO PDV', 'UF', 'ANALISTA', 'SUPERVISOR', 'SKU', 'curva',\n",
       "       'categoria', 'proj_mar', 'proj_mar+1', 'ESTOQUE ATUAL',\n",
       "       'ESTOQUE EM TRANSITO', 'PEDIDO PENDENTE', 'DDV PREVISTO', 'preço',\n",
       "       '202412', '202415', 'C-4', 'C-3', 'C-2', 'C-1', 'Atual', 'INICIO CICLO',\n",
       "       'FIM CICLO', 'DURAÇÃO', 'Descrição_y', 'chave sku2', 'chave sku1',\n",
       "       'PRECO DE COMPRA', 'PRECO DE VENDA', 'UFPRODUTO', 'PDV GINSENG',\n",
       "       'PRODUTO', 'chave', 'PICO DE VENDAS 2024',\n",
       "       'Pico Vendas Ultimos 6 ciclos', 'CRESCIMENTO_GERAL',\n",
       "       'MEDIANA DO HISTÓRICO', 'MEDIA DO HISTÓRICO', 'med_por_canal',\n",
       "       'CRESCIMENTO_FINAL', 'PV GINSENG', 'COBERTURA ATUAL', 'Estoque Total'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final_dedup.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final_dedup = df_final_dedup.drop(columns='Descrição_y')\n",
    "\n",
    "df_final_dedup = df_final_dedup.rename(columns={'Descrição_x':'Descrição'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final_dedup = df_final_dedup[['DURAÇÃO','CANAL','ANALISTA','SUPERVISOR','UF','chave','PDV','DESCRIÇÃO PDV','Código','Descrição','LINHA','Categoria','curva','Percentual de desconto consumidor','Ação consumidor',\n",
    "                                 'Percentual de desconto revendedor','Ação revendedor','C-4', 'C-3', 'C-2', 'C-1','Atual',ciclo_ano_passado ,'ESTOQUE ATUAL','ESTOQUE EM TRANSITO','PEDIDO PENDENTE','Estoque Total',\n",
    "                                 'DDV PREVISTO','COBERTURA ATUAL','proj_mar', 'proj_mar+1','MEDIA DO HISTÓRICO','PICO DE VENDAS 2024','Pico Vendas Ultimos 6 ciclos','PRECO DE COMPRA',\n",
    "                                'PRECO DE VENDA','PV GINSENG']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final_dedup['Percentual de desconto revendedor'] = np.where(df_final_dedup['Percentual de desconto revendedor'] == '0,00', df_final_dedup['Percentual de desconto consumidor'],df_final_dedup['Percentual de desconto revendedor'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'202415'"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ciclo_ano_passado"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final_dedup = df_final_dedup.drop_duplicates()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final_dedup['Valor Sugestão'] = \"\"\n",
    "\n",
    "df_final_dedup['Sugestão Abastecimento'] = \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DDV PREVISTO       float64\n",
       "COBERTURA ATUAL    float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final_dedup[['DDV PREVISTO',\n",
    "       'COBERTURA ATUAL',]].dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_final_dedup[ 'DDV PREVISTO'] =  df_final_dedup['DDV PREVISTO'].fillna(0)\n",
    "\n",
    "df_final_dedup = df_final_dedup.drop_duplicates()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>DURAÇÃO</th>\n",
       "      <th>CANAL</th>\n",
       "      <th>ANALISTA</th>\n",
       "      <th>SUPERVISOR</th>\n",
       "      <th>UF</th>\n",
       "      <th>chave</th>\n",
       "      <th>PDV</th>\n",
       "      <th>DESCRIÇÃO PDV</th>\n",
       "      <th>Código</th>\n",
       "      <th>Descrição</th>\n",
       "      <th>...</th>\n",
       "      <th>proj_mar</th>\n",
       "      <th>proj_mar+1</th>\n",
       "      <th>MEDIA DO HISTÓRICO</th>\n",
       "      <th>PICO DE VENDAS 2024</th>\n",
       "      <th>Pico Vendas Ultimos 6 ciclos</th>\n",
       "      <th>PRECO DE COMPRA</th>\n",
       "      <th>PRECO DE VENDA</th>\n",
       "      <th>PV GINSENG</th>\n",
       "      <th>Valor Sugestão</th>\n",
       "      <th>Sugestão Abastecimento</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>32171</th>\n",
       "      <td>21</td>\n",
       "      <td>VD</td>\n",
       "      <td>JEFFERSON</td>\n",
       "      <td>Juliana Vasconcelos</td>\n",
       "      <td>VDC</td>\n",
       "      <td>2371184387</td>\n",
       "      <td>23711</td>\n",
       "      <td>ER VITORIA DA CONQUISTA</td>\n",
       "      <td>84387</td>\n",
       "      <td>MALBEC DES COL V6 100ml</td>\n",
       "      <td>...</td>\n",
       "      <td>1505.0</td>\n",
       "      <td>242.0</td>\n",
       "      <td>612.388889</td>\n",
       "      <td>2734.0</td>\n",
       "      <td>857.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 39 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       DURAÇÃO CANAL   ANALISTA           SUPERVISOR   UF       chave    PDV  \\\n",
       "32171       21    VD  JEFFERSON  Juliana Vasconcelos  VDC  2371184387  23711   \n",
       "\n",
       "                 DESCRIÇÃO PDV  Código                Descrição  ... proj_mar  \\\n",
       "32171  ER VITORIA DA CONQUISTA   84387  MALBEC DES COL V6 100ml  ...   1505.0   \n",
       "\n",
       "      proj_mar+1 MEDIA DO HISTÓRICO PICO DE VENDAS 2024  \\\n",
       "32171      242.0         612.388889              2734.0   \n",
       "\n",
       "      Pico Vendas Ultimos 6 ciclos PRECO DE COMPRA PRECO DE VENDA  PV GINSENG  \\\n",
       "32171                        857.0             0.0            0.0        12.0   \n",
       "\n",
       "       Valor Sugestão  Sugestão Abastecimento  \n",
       "32171                                          \n",
       "\n",
       "[1 rows x 39 columns]"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final_dedup[df_final_dedup['chave'] == '2371184387'].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32171    88.7\n",
       "Name: DDV PREVISTO, dtype: float64"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final_dedup[df_final_dedup['chave'] == '2371184387']['DDV PREVISTO'].head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>DURAÇÃO</th>\n",
       "      <th>CANAL</th>\n",
       "      <th>ANALISTA</th>\n",
       "      <th>SUPERVISOR</th>\n",
       "      <th>UF</th>\n",
       "      <th>chave</th>\n",
       "      <th>PDV</th>\n",
       "      <th>DESCRIÇÃO PDV</th>\n",
       "      <th>Código</th>\n",
       "      <th>Descrição</th>\n",
       "      <th>...</th>\n",
       "      <th>proj_mar</th>\n",
       "      <th>proj_mar+1</th>\n",
       "      <th>MEDIA DO HISTÓRICO</th>\n",
       "      <th>PICO DE VENDAS 2024</th>\n",
       "      <th>Pico Vendas Ultimos 6 ciclos</th>\n",
       "      <th>PRECO DE COMPRA</th>\n",
       "      <th>PRECO DE VENDA</th>\n",
       "      <th>PV GINSENG</th>\n",
       "      <th>Valor Sugestão</th>\n",
       "      <th>Sugestão Abastecimento</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>21</td>\n",
       "      <td>TODOS</td>\n",
       "      <td>LUAN</td>\n",
       "      <td>Efigênia Herculano</td>\n",
       "      <td>AL</td>\n",
       "      <td>1252250052</td>\n",
       "      <td>12522</td>\n",
       "      <td>MACEIO SHOP EXP</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>11.72</td>\n",
       "      <td>37.9</td>\n",
       "      <td>0.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>21</td>\n",
       "      <td>TODOS</td>\n",
       "      <td>JEFFERSON</td>\n",
       "      <td>Maxwell Vieira</td>\n",
       "      <td>AL</td>\n",
       "      <td>1281750052</td>\n",
       "      <td>12817</td>\n",
       "      <td>SHOPPING PATIO</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.555556</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>11.72</td>\n",
       "      <td>37.9</td>\n",
       "      <td>1.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>21</td>\n",
       "      <td>TODOS</td>\n",
       "      <td>PEDRO</td>\n",
       "      <td>Maxwell Vieira</td>\n",
       "      <td>AL</td>\n",
       "      <td>1281850052</td>\n",
       "      <td>12818</td>\n",
       "      <td>GB SERRARIA</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>11.72</td>\n",
       "      <td>37.9</td>\n",
       "      <td>0.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>21</td>\n",
       "      <td>TODOS</td>\n",
       "      <td>THAYLLAN</td>\n",
       "      <td>Maxwell Vieira</td>\n",
       "      <td>AL</td>\n",
       "      <td>1282050052</td>\n",
       "      <td>12820</td>\n",
       "      <td>ATACADÃO</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>11.72</td>\n",
       "      <td>37.9</td>\n",
       "      <td>0.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>21</td>\n",
       "      <td>TODOS</td>\n",
       "      <td>MARCYARA</td>\n",
       "      <td>Efigênia Herculano</td>\n",
       "      <td>AL</td>\n",
       "      <td>1282350052</td>\n",
       "      <td>12823</td>\n",
       "      <td>PONTA VERDE</td>\n",
       "      <td>50052</td>\n",
       "      <td>ARBO LOC DES HID CPO 75ml</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.111111</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>11.72</td>\n",
       "      <td>37.9</td>\n",
       "      <td>0.0</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 39 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   DURAÇÃO  CANAL   ANALISTA          SUPERVISOR  UF       chave    PDV  \\\n",
       "0       21  TODOS       LUAN  Efigênia Herculano  AL  1252250052  12522   \n",
       "1       21  TODOS  JEFFERSON      Maxwell Vieira  AL  1281750052  12817   \n",
       "2       21  TODOS      PEDRO      Maxwell Vieira  AL  1281850052  12818   \n",
       "3       21  TODOS   THAYLLAN      Maxwell Vieira  AL  1282050052  12820   \n",
       "4       21  TODOS   MARCYARA  Efigênia Herculano  AL  1282350052  12823   \n",
       "\n",
       "     DESCRIÇÃO PDV  Código                  Descrição  ... proj_mar  \\\n",
       "0  MACEIO SHOP EXP   50052  ARBO LOC DES HID CPO 75ml  ...      1.0   \n",
       "1   SHOPPING PATIO   50052  ARBO LOC DES HID CPO 75ml  ...      1.0   \n",
       "2      GB SERRARIA   50052  ARBO LOC DES HID CPO 75ml  ...      0.0   \n",
       "3         ATACADÃO   50052  ARBO LOC DES HID CPO 75ml  ...      0.0   \n",
       "4      PONTA VERDE   50052  ARBO LOC DES HID CPO 75ml  ...      0.0   \n",
       "\n",
       "  proj_mar+1 MEDIA DO HISTÓRICO PICO DE VENDAS 2024  \\\n",
       "0        0.0           0.111111                 2.0   \n",
       "1        0.0           0.555556                 5.0   \n",
       "2        0.0           0.055556                 1.0   \n",
       "3        0.0           0.055556                 1.0   \n",
       "4        0.0           0.111111                 1.0   \n",
       "\n",
       "  Pico Vendas Ultimos 6 ciclos PRECO DE COMPRA PRECO DE VENDA  PV GINSENG  \\\n",
       "0                          0.0           11.72           37.9         0.0   \n",
       "1                          0.0           11.72           37.9         1.0   \n",
       "2                          1.0           11.72           37.9         0.0   \n",
       "3                          0.0           11.72           37.9         0.0   \n",
       "4                          0.0           11.72           37.9         0.0   \n",
       "\n",
       "   Valor Sugestão  Sugestão Abastecimento  \n",
       "0                                          \n",
       "1                                          \n",
       "2                                          \n",
       "3                                          \n",
       "4                                          \n",
       "\n",
       "[5 rows x 39 columns]"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_final_dedup.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['DURAÇÃO', 'CANAL', 'ANALISTA', 'SUPERVISOR', 'UF', 'chave', 'PDV',\n",
       "       'DESCRIÇÃO PDV', 'Código', 'Descrição', 'LINHA', 'Categoria', 'curva',\n",
       "       'Percentual de desconto consumidor', 'Ação consumidor',\n",
       "       'Percentual de desconto revendedor', 'Ação revendedor', 'C-4', 'C-3',\n",
       "       'C-2', 'C-1', 'Atual', '202415', 'ESTOQUE ATUAL', 'ESTOQUE EM TRANSITO',\n",
       "       'PEDIDO PENDENTE', 'Estoque Total', 'DDV PREVISTO', 'COBERTURA ATUAL',\n",
       "       'proj_mar', 'proj_mar+1', 'MEDIA DO HISTÓRICO', 'PICO DE VENDAS 2024',\n",
       "       'Pico Vendas Ultimos 6 ciclos', 'PRECO DE COMPRA', 'PRECO DE VENDA',\n",
       "       'PV GINSENG', 'Valor Sugestão', 'Sugestão Abastecimento',\n",
       "       'CANAL_CERTO'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_pdv_origi['PDV'] = df_pdv_origi['PDV'].astype('Int64')\n",
    "\n",
    "\n",
    "\n",
    "df_pdv_origi = df_pdv_origi.rename(columns={'CANAL':'CANAL_CERTO'})\n",
    "\n",
    "df_final_dedup= pd.merge(df_final_dedup,df_pdv_origi[['PDV','CANAL_CERTO']],on='PDV',how='inner')\n",
    "\n",
    "df_final_dedup['CANAL']= df_final_dedup['CANAL_CERTO']\n",
    "\n",
    "\n",
    "df_final_dedup.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Export to Excel\n",
    "output_file = f'C:\\\\Users\\\\joao.herculano\\\\Documents\\\\promoção_BOTI_{ciclo_mais2}.{hoje}.xlsx'\n",
    "with pd.ExcelWriter(output_file, engine='openpyxl') as writer:\n",
    "    df_final_dedup.to_excel(writer, index=False)\n",
    "\n",
    "# Apply styles\n",
    "wb = load_workbook(output_file)\n",
    "ws = wb['Sheet1']\n",
    "\n",
    "# Style header\n",
    "header_fill = PatternFill(start_color='ADD8E6', end_color='ADD8E6', fill_type='solid')  # Light Blue\n",
    "header_font = Font(color='FFFFFF', bold=True)  # White & Bold\n",
    "\n",
    "for cell in ws[1]:\n",
    "    cell.fill = header_fill\n",
    "    cell.font = header_font\n",
    "\n",
    "# Style rows: gray/white alternating\n",
    "gray_fill = PatternFill(start_color='DDDDDD', end_color='DDDDDD', fill_type='solid')  # Light gray\n",
    "\n",
    "for i, row in enumerate(ws.iter_rows(min_row=2, max_row=ws.max_row), start=2):\n",
    "    if i % 2 == 0:\n",
    "        for cell in row:\n",
    "            cell.fill = gray_fill\n",
    "\n",
    "# Save styled workbook\n",
    "wb.save(output_file)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "coisas q saem\n",
    "\n",
    "Lançamento\n",
    "Subcategoria\n",
    "Projeção Próximo Ciclo \n",
    "Promoção Próximo Ciclo\n",
    "Compra inteligente semanal/Sugestão de compra\n",
    "Compra inteligente Próximo Ciclo\n",
    "Planograma\n",
    "Carteira Bloqueada Para Novos Pedidos\n",
    "Quantidade por caixa\n",
    "Preço Sell In\n",
    "Quantidade\n",
    "Item analisado\n",
    "Tipo Preço\n",
    "\n",
    ">>>>>>>>>>>>>NAO ESTÁ PEGANDO O MERGE COM O DF_ESTOQUE\n",
    "\n",
    "\n",
    "CRIAR PROJEÇÃO DE VENDA DO CICLO ATUAL\n",
    "Compra inteligente Próximo Ciclo + 1 >>>>>>> RENAME PRA NOROMAL"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
