Ruptura_Projetada/Estudo Curva/estudo_curva.ipynb
2025-10-24 15:54:54 -03:00

736 lines
21 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "849b6b4d",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"import pyodbc\n",
"import configparser\n",
"\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",
")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "62fc9678",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\joao.herculano\\AppData\\Local\\Temp\\ipykernel_16792\\1235837546.py:16: 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_vendas = pd.read_sql(query, conn)\n"
]
}
],
"source": [
"query = '''\n",
"select\n",
"\tbvb.[DATA],\n",
"\tbvb.pdv,\n",
"\tbvb.SKU ,\n",
"\tcast(replace(bvb.VENDAS,'.','') as int) as Vendas,\n",
"\tem.ORIGEM\n",
"from base_vendas_bi bvb \n",
"left join (\n",
"select *\n",
"from\n",
"estoque_mar\n",
"where origem is not null) em on cast(em.SKU as int) = cast(replace(bvb.SKU,'.','') as int) and cast( em.pdv as int) = cast(bvb.PDV as int)\n",
"WHERE EM.CATEGORIA not in ('SUPORTE À VENDA','EMBALAGENS') AND bvb.[DATA] >'2025-04-14'\n",
"'''\n",
"df_vendas = pd.read_sql(query, conn)\n",
"conn.close()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6c7089b1",
"metadata": {},
"outputs": [],
"source": [
"df_vendas['pdv'] = np.where(df_vendas['pdv'] == '23703','23708',df_vendas['pdv'])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "834f4c3f",
"metadata": {},
"outputs": [],
"source": [
"df_vendas['DATA'] = pd.to_datetime(df_vendas['DATA'])\n",
"\n",
"df_vendas['DATA_MES'] = pd.to_datetime(df_vendas['DATA'], dayfirst=True, errors='coerce').dt.to_period('M').astype(str)\n",
"\n",
"df_vendas['Vendas'] = df_vendas['Vendas'].astype('Int64')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "6f94c57e",
"metadata": {},
"outputs": [],
"source": [
"df_tabela = pd.read_excel(r\"C:\\Users\\joao.herculano\\Documents\\compilado_tab_pedido.xlsx\")\n",
"\n",
"df_tabela['extracao_desc'] = df_tabela['Descrição'].str.split(' ').str[0]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "532d718d",
"metadata": {},
"outputs": [],
"source": [
"df_tabela['LINHA'] = np.where(df_tabela['MARCA'] == 'EUDORA',df_tabela['extracao_desc'],df_tabela['LINHA'])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "d8116439",
"metadata": {},
"outputs": [],
"source": [
"df_tabela = df_tabela.groupby(['Nome da Origem','SKU1', 'SKU2','Descrição', 'MARCA','CATEGORIA', 'LINHA','Tipo Preço','extracao_desc'])[['PC','PV']].max().reset_index()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "117ee782",
"metadata": {},
"outputs": [],
"source": [
"df_vendas2 = pd.merge(df_vendas,df_tabela,left_on='SKU',right_on='SKU2',how='left')\n",
"df_vendas2 = df_vendas2.drop_duplicates()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "374e679c",
"metadata": {},
"outputs": [],
"source": [
"df_vendas2['preço_venda'] = df_vendas2['Vendas']*df_vendas2['PV']"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "caecc276",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1568926, 18)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_vendas2.shape"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "619bc34d",
"metadata": {},
"outputs": [],
"source": [
"df_vendas2['PV'] = df_vendas2['PV'].astype('str').str.replace(',','')\n",
"df_vendas2['PV'] = df_vendas2['PV'].astype('float')"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "9e0ef539",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.float64(306748811.9699977)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_vendas2['preço_venda'].sum()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "42c56b2f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.int64(16832)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_vendas2[(df_vendas2['SKU']==84387)& (df_vendas2['pdv']=='20998')]['Vendas'].sum()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "d1ce7af2",
"metadata": {},
"outputs": [],
"source": [
"# Agrupar vendas por PDV, Marca e SKU\n",
"vendas_por_pdv_sku = df_vendas2.groupby(['pdv', 'MARCA', 'SKU'])['preço_venda'].sum().reset_index()\n",
"\n",
"# Ordenar por PDV, MARCA e vendas (maior para menor)\n",
"vendas_por_pdv_sku = vendas_por_pdv_sku.sort_values(['pdv', 'MARCA', 'preço_venda'], ascending=[True, True, False])\n",
"\n",
"# Calcular percentual acumulado DENTRO de cada PDV e MARCA\n",
"vendas_por_pdv_sku['percentual_acumulado'] = vendas_por_pdv_sku.groupby(['pdv', 'MARCA'])['preço_venda'].transform(\n",
" lambda x: (x.cumsum() / x.sum()) * 100\n",
")\n",
"\n",
"# Criar coluna CLASSE\n",
"vendas_por_pdv_sku['CLASSE'] = vendas_por_pdv_sku['percentual_acumulado'].apply(\n",
" lambda x: 'A' if x <= 80 else ('B' if x <= 95 else 'C')\n",
")\n",
"\n",
"\n",
"# Visualizar resultado\n",
"vendas_por_pdv2 =vendas_por_pdv_sku.reset_index().sort_values(['pdv','percentual_acumulado'])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "de01395e",
"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>index</th>\n",
" <th>PDV</th>\n",
" <th>MARCA</th>\n",
" <th>SKU</th>\n",
" <th>preço_venda</th>\n",
" <th>percentual_acumulado</th>\n",
" <th>CLASSE</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>972</td>\n",
" <td>12522</td>\n",
" <td>BOTICARIO</td>\n",
" <td>84387</td>\n",
" <td>31384.3</td>\n",
" <td>2.360871</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>787</td>\n",
" <td>12522</td>\n",
" <td>BOTICARIO</td>\n",
" <td>75792</td>\n",
" <td>23384.4</td>\n",
" <td>4.119953</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>747</td>\n",
" <td>12522</td>\n",
" <td>BOTICARIO</td>\n",
" <td>74043</td>\n",
" <td>22028.4</td>\n",
" <td>5.77703</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>315</td>\n",
" <td>12522</td>\n",
" <td>BOTICARIO</td>\n",
" <td>50677</td>\n",
" <td>20946.9</td>\n",
" <td>7.352751</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>816</td>\n",
" <td>12522</td>\n",
" <td>BOTICARIO</td>\n",
" <td>77524</td>\n",
" <td>19423.3</td>\n",
" <td>8.813861</td>\n",
" <td>A</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",
" </tr>\n",
" <tr>\n",
" <th>107809</th>\n",
" <td>107809</td>\n",
" <td>5699</td>\n",
" <td>BOTICARIO</td>\n",
" <td>87421</td>\n",
" <td>0.0</td>\n",
" <td>100.0</td>\n",
" <td>C</td>\n",
" </tr>\n",
" <tr>\n",
" <th>107810</th>\n",
" <td>107810</td>\n",
" <td>5699</td>\n",
" <td>BOTICARIO</td>\n",
" <td>87422</td>\n",
" <td>0.0</td>\n",
" <td>100.0</td>\n",
" <td>C</td>\n",
" </tr>\n",
" <tr>\n",
" <th>107811</th>\n",
" <td>107811</td>\n",
" <td>5699</td>\n",
" <td>BOTICARIO</td>\n",
" <td>87423</td>\n",
" <td>0.0</td>\n",
" <td>100.0</td>\n",
" <td>C</td>\n",
" </tr>\n",
" <tr>\n",
" <th>107812</th>\n",
" <td>107812</td>\n",
" <td>5699</td>\n",
" <td>BOTICARIO</td>\n",
" <td>87424</td>\n",
" <td>0.0</td>\n",
" <td>100.0</td>\n",
" <td>C</td>\n",
" </tr>\n",
" <tr>\n",
" <th>107813</th>\n",
" <td>107813</td>\n",
" <td>5699</td>\n",
" <td>EUDORA</td>\n",
" <td>53095</td>\n",
" <td>0.0</td>\n",
" <td>NaN</td>\n",
" <td>C</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>107814 rows × 7 columns</p>\n",
"</div>"
],
"text/plain": [
" index PDV MARCA SKU preço_venda percentual_acumulado \\\n",
"0 972 12522 BOTICARIO 84387 31384.3 2.360871 \n",
"1 787 12522 BOTICARIO 75792 23384.4 4.119953 \n",
"2 747 12522 BOTICARIO 74043 22028.4 5.77703 \n",
"3 315 12522 BOTICARIO 50677 20946.9 7.352751 \n",
"4 816 12522 BOTICARIO 77524 19423.3 8.813861 \n",
"... ... ... ... ... ... ... \n",
"107809 107809 5699 BOTICARIO 87421 0.0 100.0 \n",
"107810 107810 5699 BOTICARIO 87422 0.0 100.0 \n",
"107811 107811 5699 BOTICARIO 87423 0.0 100.0 \n",
"107812 107812 5699 BOTICARIO 87424 0.0 100.0 \n",
"107813 107813 5699 EUDORA 53095 0.0 NaN \n",
"\n",
" CLASSE \n",
"0 A \n",
"1 A \n",
"2 A \n",
"3 A \n",
"4 A \n",
"... ... \n",
"107809 C \n",
"107810 C \n",
"107811 C \n",
"107812 C \n",
"107813 C \n",
"\n",
"[107814 rows x 7 columns]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vendas_por_pdv2 = vendas_por_pdv2.rename(columns={'pdv':'PDV'})\n",
"\n",
"vendas_por_pdv2"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "f3ae19e1",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\joao.herculano\\AppData\\Local\\Temp\\ipykernel_16792\\145927781.py:15: 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_estoque = pd.read_sql(query, 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",
"query = '''\n",
"select\n",
"em.PDV,\n",
"em.SKU,\n",
"em.CLASSE as CLASSE_MAR\n",
"from estoque_mar em\n",
"'''\n",
"df_estoque = pd.read_sql(query, conn)\n",
"conn.close()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "a7b971ac",
"metadata": {},
"outputs": [],
"source": [
"df_estoque['SKU'] = df_estoque['SKU'].astype('Int64')"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "d02a8433",
"metadata": {},
"outputs": [],
"source": [
"vendas_por_pdv2 = vendas_por_pdv2.rename(columns={'pdv':'PDV'})"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "3aee39ce",
"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>index</th>\n",
" <th>PDV</th>\n",
" <th>MARCA</th>\n",
" <th>SKU</th>\n",
" <th>preço_venda</th>\n",
" <th>percentual_acumulado</th>\n",
" <th>CLASSE</th>\n",
" <th>CLASSE_MAR</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>972</td>\n",
" <td>12522</td>\n",
" <td>BOTICARIO</td>\n",
" <td>84387</td>\n",
" <td>31384.3</td>\n",
" <td>2.360871</td>\n",
" <td>A</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>787</td>\n",
" <td>12522</td>\n",
" <td>BOTICARIO</td>\n",
" <td>75792</td>\n",
" <td>23384.4</td>\n",
" <td>4.119953</td>\n",
" <td>A</td>\n",
" <td>E</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>747</td>\n",
" <td>12522</td>\n",
" <td>BOTICARIO</td>\n",
" <td>74043</td>\n",
" <td>22028.4</td>\n",
" <td>5.77703</td>\n",
" <td>A</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>315</td>\n",
" <td>12522</td>\n",
" <td>BOTICARIO</td>\n",
" <td>50677</td>\n",
" <td>20946.9</td>\n",
" <td>7.352751</td>\n",
" <td>A</td>\n",
" <td>A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>816</td>\n",
" <td>12522</td>\n",
" <td>BOTICARIO</td>\n",
" <td>77524</td>\n",
" <td>19423.3</td>\n",
" <td>8.813861</td>\n",
" <td>A</td>\n",
" <td>A</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",
" </tr>\n",
" <tr>\n",
" <th>107809</th>\n",
" <td>107809</td>\n",
" <td>5699</td>\n",
" <td>BOTICARIO</td>\n",
" <td>87421</td>\n",
" <td>0.0</td>\n",
" <td>100.0</td>\n",
" <td>C</td>\n",
" <td>E</td>\n",
" </tr>\n",
" <tr>\n",
" <th>107810</th>\n",
" <td>107810</td>\n",
" <td>5699</td>\n",
" <td>BOTICARIO</td>\n",
" <td>87422</td>\n",
" <td>0.0</td>\n",
" <td>100.0</td>\n",
" <td>C</td>\n",
" <td>E</td>\n",
" </tr>\n",
" <tr>\n",
" <th>107811</th>\n",
" <td>107811</td>\n",
" <td>5699</td>\n",
" <td>BOTICARIO</td>\n",
" <td>87423</td>\n",
" <td>0.0</td>\n",
" <td>100.0</td>\n",
" <td>C</td>\n",
" <td>E</td>\n",
" </tr>\n",
" <tr>\n",
" <th>107812</th>\n",
" <td>107812</td>\n",
" <td>5699</td>\n",
" <td>BOTICARIO</td>\n",
" <td>87424</td>\n",
" <td>0.0</td>\n",
" <td>100.0</td>\n",
" <td>C</td>\n",
" <td>E</td>\n",
" </tr>\n",
" <tr>\n",
" <th>107813</th>\n",
" <td>107813</td>\n",
" <td>5699</td>\n",
" <td>EUDORA</td>\n",
" <td>53095</td>\n",
" <td>0.0</td>\n",
" <td>NaN</td>\n",
" <td>C</td>\n",
" <td>E</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>107814 rows × 8 columns</p>\n",
"</div>"
],
"text/plain": [
" index PDV MARCA SKU preço_venda percentual_acumulado \\\n",
"0 972 12522 BOTICARIO 84387 31384.3 2.360871 \n",
"1 787 12522 BOTICARIO 75792 23384.4 4.119953 \n",
"2 747 12522 BOTICARIO 74043 22028.4 5.77703 \n",
"3 315 12522 BOTICARIO 50677 20946.9 7.352751 \n",
"4 816 12522 BOTICARIO 77524 19423.3 8.813861 \n",
"... ... ... ... ... ... ... \n",
"107809 107809 5699 BOTICARIO 87421 0.0 100.0 \n",
"107810 107810 5699 BOTICARIO 87422 0.0 100.0 \n",
"107811 107811 5699 BOTICARIO 87423 0.0 100.0 \n",
"107812 107812 5699 BOTICARIO 87424 0.0 100.0 \n",
"107813 107813 5699 EUDORA 53095 0.0 NaN \n",
"\n",
" CLASSE CLASSE_MAR \n",
"0 A A \n",
"1 A E \n",
"2 A A \n",
"3 A A \n",
"4 A A \n",
"... ... ... \n",
"107809 C E \n",
"107810 C E \n",
"107811 C E \n",
"107812 C E \n",
"107813 C E \n",
"\n",
"[107814 rows x 8 columns]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vendas_por_pdv3 = pd.merge(vendas_por_pdv2,df_estoque,on=['PDV','SKU'],how='left')\n",
"\n",
"vendas_por_pdv3"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "44067ef7",
"metadata": {},
"outputs": [],
"source": [
"vendas_por_pdv3.to_excel(r'C:\\Users\\joao.herculano\\GRUPO GINSENG\\Assistência Suprimentos - 2025\\CODIGOS\\Estudo Curva\\avaliação_curva.xlsx',index=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "26a70844",
"metadata": {},
"outputs": [],
"source": []
}
],
"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": 5
}