{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "bd78c2e2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(105809, 4)\n"
]
}
],
"source": [
"import os\n",
"import glob\n",
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"# Caminho da pasta com os arquivos Excel\n",
"folder_path = r\"C:\\Users\\joao.herculano\\Documents\\analise de previsao ginseng x mar\\promoções\"\n",
"\n",
"# Padrão para localizar todos os arquivos .xlsx\n",
"excel_files = glob.glob(os.path.join(folder_path, '*.xlsx'))\n",
"\n",
"# Lista para armazenar os DataFrames válidos\n",
"dataframes = []\n",
"\n",
"# Colunas obrigatórias\n",
"required_columns = ['PDV',\n",
"'PRODUTO',\n",
"'COMPRA FINAL']\n",
"\n",
"# Para cada arquivo Excel\n",
"for file in excel_files:\n",
" success = False\n",
" for header_row in range(10):\n",
" try:\n",
" df = pd.read_excel(file, header=header_row)\n",
" if all(col in df.columns for col in required_columns):\n",
" if 'Sugestão Pedro' in df.columns or 'Sugestão Laura' in df.columns:\n",
" print(file)\n",
" df = df[required_columns]\n",
" df['arquivo fonte'] = file\n",
" dataframes.append(df)\n",
" success = True\n",
" break\n",
" except Exception as e:\n",
" continue\n",
" if not success:\n",
" print(f\"Arquivo ignorado (colunas ausentes ou erro na leitura): {file}\")\n",
"\n",
"# Concatena os DataFrames válidos\n",
"retorno_analista = pd.concat(dataframes, ignore_index=True)\n",
"\n",
"# Exibe a forma do DataFrame resultante\n",
"print(retorno_analista.shape)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8f30f0f6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(105809, 4)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"retorno_analista = retorno_analista.drop_duplicates()\n",
"retorno_analista.shape"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "3288bda7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['PDV', 'PRODUTO', 'COMPRA FINAL', 'arquivo fonte'], dtype='object')"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"retorno_analista.columns"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d3e20e1d",
"metadata": {},
"outputs": [],
"source": [
"retorno_analista = retorno_analista.fillna(0)\n",
"#retorno_analista = retorno_analista[retorno_analista['COMPRA FINAL']!=0]\n",
"retorno_analista['COMPRA FINAL'] = retorno_analista['COMPRA FINAL'].astype('Int64')\n",
"\n",
"retorno_analista2 = retorno_analista.groupby(['PDV','PRODUTO','arquivo fonte'])['COMPRA FINAL'].max().reset_index()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "96504ca6",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" PDV | \n",
" PRODUTO | \n",
" arquivo fonte | \n",
" COMPRA FINAL | \n",
"
\n",
" \n",
" \n",
" \n",
" | 57211 | \n",
" 20997 | \n",
" 84387 | \n",
" C:\\Users\\joao.herculano\\Documents\\analise de p... | \n",
" 3000 | \n",
"
\n",
" \n",
" | 55854 | \n",
" 20997 | \n",
" 22249 | \n",
" C:\\Users\\joao.herculano\\Documents\\analise de p... | \n",
" 1600 | \n",
"
\n",
" \n",
" | 54453 | \n",
" 20996 | \n",
" 48062 | \n",
" C:\\Users\\joao.herculano\\Documents\\analise de p... | \n",
" 1500 | \n",
"
\n",
" \n",
" | 49726 | \n",
" 20993 | \n",
" 19734 | \n",
" C:\\Users\\joao.herculano\\Documents\\analise de p... | \n",
" 1450 | \n",
"
\n",
" \n",
" | 50060 | \n",
" 20993 | \n",
" 50677 | \n",
" C:\\Users\\joao.herculano\\Documents\\analise de p... | \n",
" 1450 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" PDV PRODUTO arquivo fonte \\\n",
"57211 20997 84387 C:\\Users\\joao.herculano\\Documents\\analise de p... \n",
"55854 20997 22249 C:\\Users\\joao.herculano\\Documents\\analise de p... \n",
"54453 20996 48062 C:\\Users\\joao.herculano\\Documents\\analise de p... \n",
"49726 20993 19734 C:\\Users\\joao.herculano\\Documents\\analise de p... \n",
"50060 20993 50677 C:\\Users\\joao.herculano\\Documents\\analise de p... \n",
"\n",
" COMPRA FINAL \n",
"57211 3000 \n",
"55854 1600 \n",
"54453 1500 \n",
"49726 1450 \n",
"50060 1450 "
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"retorno_analista2.sort_values('COMPRA FINAL',ascending=False).head()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "e66c606c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" chave | \n",
" PDV | \n",
" PRODUTO | \n",
" COMPRA FINAL | \n",
" arquivo fonte | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 35461004 | \n",
" 3546 | \n",
" 1004 | \n",
" 0 | \n",
" C:\\Users\\joao.herculano\\Documents\\analise de p... | \n",
"
\n",
" \n",
" | 1 | \n",
" 35461004 | \n",
" 3546 | \n",
" 1004 | \n",
" 0 | \n",
" C:\\Users\\joao.herculano\\Documents\\analise de p... | \n",
"
\n",
" \n",
" | 2 | \n",
" 35461004 | \n",
" 3546 | \n",
" 1004 | \n",
" 0 | \n",
" C:\\Users\\joao.herculano\\Documents\\analise de p... | \n",
"
\n",
" \n",
" | 3 | \n",
" 35461676 | \n",
" 3546 | \n",
" 1676 | \n",
" 0 | \n",
" C:\\Users\\joao.herculano\\Documents\\analise de p... | \n",
"
\n",
" \n",
" | 4 | \n",
" 35461680 | \n",
" 3546 | \n",
" 1680 | \n",
" 0 | \n",
" C:\\Users\\joao.herculano\\Documents\\analise de p... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" chave PDV PRODUTO COMPRA FINAL \\\n",
"0 35461004 3546 1004 0 \n",
"1 35461004 3546 1004 0 \n",
"2 35461004 3546 1004 0 \n",
"3 35461676 3546 1676 0 \n",
"4 35461680 3546 1680 0 \n",
"\n",
" arquivo fonte \n",
"0 C:\\Users\\joao.herculano\\Documents\\analise de p... \n",
"1 C:\\Users\\joao.herculano\\Documents\\analise de p... \n",
"2 C:\\Users\\joao.herculano\\Documents\\analise de p... \n",
"3 C:\\Users\\joao.herculano\\Documents\\analise de p... \n",
"4 C:\\Users\\joao.herculano\\Documents\\analise de p... "
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"retorno_analista2['chave'] = retorno_analista2['PDV'].astype('str')+retorno_analista2['PRODUTO'].astype('str')\n",
"\n",
"retorno_analista2.head()\n",
"retorno_analista2 = retorno_analista2[['chave','PDV',\t'PRODUTO',\t'COMPRA FINAL','arquivo fonte'\t]]\n",
"\n",
"retorno_analista2.head()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "527c33cf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 35461004\n",
"1 35461004\n",
"2 35461004\n",
"3 35461676\n",
"4 35461680\n",
"Name: chave, dtype: object"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"retorno_analista2['chave'] = retorno_analista2['chave'].str.replace('.0','')\n",
"\n",
"retorno_analista2['chave'].head()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "2819f6d3",
"metadata": {},
"outputs": [],
"source": [
"retorno_analista2.to_excel(r'C:\\Users\\joao.herculano\\Documents\\analise de previsao ginseng x mar\\CompiladoPromo.xlsx',index=False)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1d8b6fc0",
"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
}