{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "dd143b00-1989-44cf-8558-a30087d17f70",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import s3fs\n",
    "import warnings\n",
    "from datetime import date, timedelta, datetime\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.dates as mdates\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "08c63120-1b56-4145-9014-18a637b22876",
   "metadata": {},
   "outputs": [],
   "source": [
    "exec(open('../../0_KPI_functions.py').read())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f8bd679d-fa76-49d4-9ec1-9f15516f16d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Ignore warning\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec9e996d-3eae-4836-8cf5-268e5dc0d672",
   "metadata": {},
   "source": [
    "# Statistiques descriptives : compagnies sport"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43f81515-fbd0-49c0-b3f8-0e0fb663e2c1",
   "metadata": {},
   "source": [
    "## Importations et chargement des données"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "945c59bb-05b4-4f21-82f0-0db40d7957b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create filesystem object\n",
    "S3_ENDPOINT_URL = \"https://\" + os.environ[\"AWS_S3_ENDPOINT\"]\n",
    "fs = s3fs.S3FileSystem(client_kwargs={'endpoint_url': S3_ENDPOINT_URL})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "41a67995-0a08-45c0-bbad-6e6cee5474c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File path :  projet-bdc2324-team1/0_Input/Company_5/customerplus_cleaned.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_5/campaigns_information.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_5/products_purchased_reduced.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_5/target_information.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_6/customerplus_cleaned.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_6/campaigns_information.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_6/products_purchased_reduced.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_6/target_information.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_7/customerplus_cleaned.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_7/campaigns_information.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_7/products_purchased_reduced.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_7/target_information.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_8/customerplus_cleaned.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_8/campaigns_information.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_8/products_purchased_reduced.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_8/target_information.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_9/customerplus_cleaned.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_9/campaigns_information.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_9/products_purchased_reduced.csv\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_9/target_information.csv\n"
     ]
    }
   ],
   "source": [
    "# création des bases contenant les KPI pour les 5 compagnies de spectacle\n",
    "\n",
    "# liste des compagnies de spectacle\n",
    "nb_compagnie=['5','6','7','8','9']\n",
    "\n",
    "customer_sport = pd.DataFrame()\n",
    "campaigns_sport_brut = pd.DataFrame()\n",
    "campaigns_sport_kpi = pd.DataFrame()\n",
    "products_sport = pd.DataFrame()\n",
    "tickets_sport = pd.DataFrame()\n",
    "\n",
    "# début de la boucle permettant de générer des datasets agrégés pour les 5 compagnies de spectacle\n",
    "for directory_path in nb_compagnie:\n",
    "    df_customerplus_clean_0 = display_databases(directory_path, file_name = \"customerplus_cleaned\")\n",
    "    df_campaigns_brut = display_databases(directory_path, file_name = \"campaigns_information\", datetime_col = ['opened_at', 'sent_at', 'campaign_sent_at'])\n",
    "    df_products_purchased_reduced = display_databases(directory_path, file_name = \"products_purchased_reduced\", datetime_col = ['purchase_date'])\n",
    "    df_target_information = display_databases(directory_path, file_name = \"target_information\")\n",
    "    \n",
    "    df_campaigns_kpi = campaigns_kpi_function(campaigns_information = df_campaigns_information) \n",
    "    df_tickets_kpi = tickets_kpi_function(tickets_information = df_products_purchased_reduced)\n",
    "    df_customerplus_clean = customerplus_kpi_function(customerplus_clean = df_customerplus_clean_0)\n",
    "\n",
    "        \n",
    "# creation de la colonne Number compagnie, qui permettra d'agréger les résultats\n",
    "    df_tickets_kpi[\"number_company\"]=int(directory_path)\n",
    "    df_campaigns_brut[\"number_company\"]=int(directory_path)\n",
    "    df_campaigns_kpi[\"number_company\"]=int(directory_path)\n",
    "    df_customerplus_clean[\"number_company\"]=int(directory_path)\n",
    "    df_target_information[\"number_company\"]=int(directory_path)\n",
    "\n",
    "# Traitement des index\n",
    "    df_tickets_kpi[\"customer_id\"]= directory_path + '_' +  df_tickets_kpi['customer_id'].astype('str')\n",
    "    df_campaigns_brut[\"customer_id\"]= directory_path + '_' +  df_campaigns_brut['customer_id'].astype('str')\n",
    "    df_campaigns_kpi[\"customer_id\"]= directory_path + '_' +  df_campaigns_kpi['customer_id'].astype('str') \n",
    "    df_customerplus_clean[\"customer_id\"]= directory_path + '_' +  df_customerplus_clean['customer_id'].astype('str') \n",
    "    df_products_purchased_reduced[\"customer_id\"]= directory_path + '_' +  df_products_purchased_reduced['customer_id'].astype('str') \n",
    "\n",
    "# Concaténation\n",
    "    customer_sport = pd.concat([customer_sport, df_customerplus_clean], ignore_index=True)\n",
    "    campaigns_sport_kpi = pd.concat([campaigns_sport_kpi, df_campaigns_kpi], ignore_index=True)\n",
    "    campaigns_sport_brut = pd.concat([campaigns_sport_brut, df_campaigns_brut], ignore_index=True) \n",
    "    tickets_sport = pd.concat([tickets_sport, df_tickets_kpi], ignore_index=True)\n",
    "    products_sport = pd.concat([products_sport, df_products_purchased_reduced], ignore_index=True)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "866a137c-7385-4f12-9349-b0202c71dff3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Construct dataset concerning only customer after start date\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62922029-8071-402e-8115-c145a2874a2f",
   "metadata": {},
   "source": [
    "## Statistiques descriptives"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d347bca9-3041-4414-b18e-19b626998a3e",
   "metadata": {},
   "source": [
    "### 0. Détection du client anonyme (outlier) - utile pour la section 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c4d4b2ad-8a3c-477b-bc52-dd4860527bfe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sport_comp = tickets_sport['number_company'].unique()\n",
    "sport_comp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "97a9e235-1c04-46bf-9f3c-5496e141cc40",
   "metadata": {},
   "outputs": [],
   "source": [
    "def outlier_detection(company_list, show_diagram=False):\n",
    "\n",
    "    outlier_list = list()\n",
    "    \n",
    "    for company in company_list:\n",
    "        total_amount_share = tickets_sport[tickets_sport['number_company']==company].groupby('customer_id')['total_amount'].sum().reset_index()\n",
    "        total_amount_share['CA'] = total_amount_share['total_amount'].sum()\n",
    "        total_amount_share['share_total_amount'] = total_amount_share['total_amount']/total_amount_share['CA']\n",
    "        \n",
    "        total_amount_share_index = total_amount_share.set_index('customer_id')\n",
    "        df_circulaire = total_amount_share_index['total_amount'].sort_values(axis = 0, ascending = False)\n",
    "        top = df_circulaire[:1]\n",
    "        outlier_list.append(top.index[0])\n",
    "        rest = df_circulaire[1:]\n",
    "    \n",
    "        # Calculez la somme du reste\n",
    "        rest_sum = rest.sum()\n",
    "        \n",
    "        # Créez une nouvelle série avec les cinq plus grandes parts et 'Autre'\n",
    "        new_series = pd.concat([top, pd.Series([rest_sum], index=['Autre'])])\n",
    "        \n",
    "        # Créez le graphique circulaire\n",
    "        if show_diagram:\n",
    "            plt.figure(figsize=(3, 3))\n",
    "            plt.pie(new_series, labels=new_series.index, autopct='%1.1f%%', startangle=140, pctdistance=0.5)\n",
    "            plt.axis('equal')  # Assurez-vous que le graphique est un cercle\n",
    "            plt.title(f'Répartition des montants totaux pour la compagnie {company}')\n",
    "            plt.show()\n",
    "    return outlier_list\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "770cd3fc-bfe2-4a69-89bc-0eb946311130",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['5_191835', '6_591412', '7_49632', '8_1942', '9_19683']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "outlier_list = outlier_detection(sport_comp)\n",
    "outlier_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "70b6e961-c303-465e-93f4-609721d38454",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Suppression Réussie\n"
     ]
    }
   ],
   "source": [
    "# On filtre les outliers\n",
    "\n",
    "def remove_elements(lst, elements_to_remove):\n",
    "    return ''.join([x for x in lst if x not in elements_to_remove])\n",
    "    \n",
    "databases = [customer_sport, campaigns_sport, tickets_sport, products_sport]\n",
    "\n",
    "for dataset in databases:\n",
    "    dataset['customer_id'] = dataset['customer_id'].apply(lambda x: remove_elements(x, outlier_list))\n",
    "\n",
    "# On test\n",
    "\n",
    "bool = '5_191835' in customer_sport['customer_id']\n",
    "if not bool:\n",
    "    print(\"Suppression Réussie\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b54b920a-7b46-490f-ba7e-d1859055a4e3",
   "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>customer_id</th>\n",
       "      <th>street_id</th>\n",
       "      <th>structure_id</th>\n",
       "      <th>mcp_contact_id</th>\n",
       "      <th>fidelity</th>\n",
       "      <th>tenant_id</th>\n",
       "      <th>is_partner</th>\n",
       "      <th>deleted_at</th>\n",
       "      <th>gender</th>\n",
       "      <th>is_email_true</th>\n",
       "      <th>...</th>\n",
       "      <th>total_price</th>\n",
       "      <th>purchase_count</th>\n",
       "      <th>first_buying_date</th>\n",
       "      <th>country</th>\n",
       "      <th>gender_label</th>\n",
       "      <th>gender_female</th>\n",
       "      <th>gender_male</th>\n",
       "      <th>gender_other</th>\n",
       "      <th>country_fr</th>\n",
       "      <th>number_company</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5_6009745</td>\n",
       "      <td>1372685</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>1771</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>af</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5_6011228</td>\n",
       "      <td>1372685</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>1771</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>af</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5_6058950</td>\n",
       "      <td>1372685</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>1771</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>af</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5_6062404</td>\n",
       "      <td>1372685</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>1771</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>af</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5_250217</td>\n",
       "      <td>78785</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11035.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1771</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>fr</td>\n",
       "      <td>female</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 28 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  customer_id  street_id  structure_id  mcp_contact_id  fidelity  tenant_id  \\\n",
       "0   5_6009745    1372685           NaN             NaN         0       1771   \n",
       "1   5_6011228    1372685           NaN             NaN         0       1771   \n",
       "2   5_6058950    1372685           NaN             NaN         0       1771   \n",
       "3   5_6062404    1372685           NaN             NaN         0       1771   \n",
       "4    5_250217      78785           NaN         11035.0         0       1771   \n",
       "\n",
       "   is_partner  deleted_at  gender  is_email_true  ...  total_price  \\\n",
       "0       False         NaN       2           True  ...          0.0   \n",
       "1       False         NaN       2           True  ...          0.0   \n",
       "2       False         NaN       2           True  ...          0.0   \n",
       "3       False         NaN       2           True  ...          0.0   \n",
       "4       False         NaN       0           True  ...          NaN   \n",
       "\n",
       "  purchase_count  first_buying_date  country  gender_label  gender_female  \\\n",
       "0              0                NaN       af         other              0   \n",
       "1              0                NaN       af         other              0   \n",
       "2              0                NaN       af         other              0   \n",
       "3              0                NaN       af         other              0   \n",
       "4              0                NaN       fr        female              1   \n",
       "\n",
       "   gender_male  gender_other  country_fr  number_company  \n",
       "0            0             1         0.0               5  \n",
       "1            0             1         0.0               5  \n",
       "2            0             1         0.0               5  \n",
       "3            0             1         0.0               5  \n",
       "4            0             0         1.0               5  \n",
       "\n",
       "[5 rows x 28 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customer_sport.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d40fe668-e1d7-4544-9db8-02498afe65fe",
   "metadata": {},
   "source": [
    "### 1. customerplus_clean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "eec1ac0b-2502-452b-97e6-69ffb77156d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def compute_nb_clients(customer_sport):\n",
    "    company_nb_clients = customer_sport[customer_sport[\"purchase_count\"]>0].groupby(\"number_company\")[\"customer_id\"].count().reset_index()\n",
    "    plt.bar(company_nb_clients[\"number_company\"], company_nb_clients[\"customer_id\"]/1000)\n",
    "\n",
    "    # Ajout de titres et d'étiquettes\n",
    "    plt.xlabel('Company')\n",
    "    plt.ylabel(\"Nombre de clients (milliers)\")\n",
    "    plt.title(\"Nombre de clients de chaque compagnie de sport\")\n",
    "    \n",
    "    # Affichage du barplot\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "db4494e7-6f65-4f7e-bf8c-8ec321d0b02d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 24768 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "compute_nb_clients(customer_sport)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a12a59a0-edfe-4e52-8037-9b875f823b33",
   "metadata": {},
   "outputs": [],
   "source": [
    "def maximum_price_paid(customer_sport):\n",
    "    company_max_price = customer_sport.groupby(\"number_company\")[\"max_price\"].max().reset_index()\n",
    "    # Création du barplot\n",
    "    plt.bar(company_max_price[\"number_company\"], company_max_price[\"max_price\"])\n",
    "    \n",
    "    # Ajout de titres et d'étiquettes\n",
    "    plt.xlabel('Company')\n",
    "    plt.ylabel(\"Prix maximal d'un billet vendu\")\n",
    "    plt.title(\"Prix maximal de vente observé par compagnie de sport\")\n",
    "    \n",
    "    # Affichage du barplot\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2c7c2d26-4e35-4163-b771-fa4d3e8ca83e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 29448 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "maximum_price_paid(customer_sport)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90e050a5-2561-49d9-8ad8-877bdb368ed1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#def sale_canal(customer_sport)\n",
    " #   avg_supp_event = customer_sport['nb_suppliers'].mean()\n",
    "  #  avg_supp_event.plot(kind='bar')\n",
    "   # plt.xlabel(\"Type d'évènement\")\n",
    "    #plt.ylabel('Nombre de Canaux de Ventes Moyen')\n",
    "    #plt.title(\"Nombre de Canaux de Ventes Moyen utilisé par les Consommateurs par type d'évènement\")\n",
    "    #plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa4974b5-637e-43e6-86c4-ee7a3adb89d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Nombre Total de tickets achetés sur Internet par compagnie"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "597d4361-8beb-43f4-9224-8f7dc34b187c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Statistiques Descriptives company 5\n",
      "       average_price  average_price_basket  average_ticket_basket  \\\n",
      "count  145390.000000          68869.000000           68869.000000   \n",
      "mean       11.070309             65.969693               3.655202   \n",
      "std        16.353610            195.462869              13.119612   \n",
      "min         0.000000              0.000000               1.000000   \n",
      "25%         0.000000             20.000000               1.000000   \n",
      "50%         0.000000             45.000000               2.000000   \n",
      "75%        20.000000             79.500000               3.000000   \n",
      "max       500.000000          24159.405000            2139.833333   \n",
      "\n",
      "       purchase_count   total_price  \n",
      "count    471598.00000  3.950770e+05  \n",
      "mean          0.29900  2.608544e+01  \n",
      "std           7.22753  2.089636e+03  \n",
      "min           0.00000  0.000000e+00  \n",
      "25%           0.00000  0.000000e+00  \n",
      "50%           0.00000  0.000000e+00  \n",
      "75%           0.00000  0.000000e+00  \n",
      "max        3532.00000  1.262516e+06  \n",
      "Statistiques Descriptives company 6\n",
      "       average_price  average_price_basket  average_ticket_basket  \\\n",
      "count   33779.000000          33779.000000           33779.000000   \n",
      "mean       24.033859             56.711279               2.413530   \n",
      "std        21.217031             72.841926               3.763809   \n",
      "min       -52.740000          -1046.666667               1.000000   \n",
      "25%        10.000000             19.000000               1.080000   \n",
      "50%        19.333333             39.000000               2.000000   \n",
      "75%        30.000000             72.990000               3.000000   \n",
      "max       199.990000           3922.845361             309.047619   \n",
      "\n",
      "       purchase_count    total_price  \n",
      "count    79938.000000   79938.000000  \n",
      "mean         2.842090     102.251041  \n",
      "std         74.949889    4290.159858  \n",
      "min          0.000000   -3140.000000  \n",
      "25%          0.000000       0.000000  \n",
      "50%          0.000000       0.000000  \n",
      "75%          1.000000      54.980000  \n",
      "max      14750.000000  762695.290000  \n",
      "Statistiques Descriptives company 7\n",
      "       average_price  average_price_basket  average_ticket_basket  \\\n",
      "count   39524.000000          39524.000000           39524.000000   \n",
      "mean       33.110568            155.618778               3.365885   \n",
      "std        85.221328           1085.613137               6.283143   \n",
      "min         0.000000              0.000000               1.000000   \n",
      "25%        17.250000             25.000000               1.800000   \n",
      "50%        25.000000             57.676364               2.000000   \n",
      "75%        43.054691            115.837500               3.555556   \n",
      "max     10770.000000          86160.000000             400.000000   \n",
      "\n",
      "       purchase_count    total_price  \n",
      "count    68800.000000   68800.000000  \n",
      "mean         3.290029     944.593729  \n",
      "std         88.071870   12118.394731  \n",
      "min          0.000000       0.000000  \n",
      "25%          0.000000       0.000000  \n",
      "50%          1.000000       9.000000  \n",
      "75%          2.000000     132.000000  \n",
      "max      22934.000000  940874.200000  \n",
      "Statistiques Descriptives company 8\n",
      "       average_price  average_price_basket  average_ticket_basket  \\\n",
      "count  129198.000000         129198.000000          129198.000000   \n",
      "mean       18.409977             38.492520               2.258036   \n",
      "std        19.159059             71.136628               5.270858   \n",
      "min       -20.000000          -1545.000000               1.000000   \n",
      "25%         0.000000              0.000000               1.000000   \n",
      "50%        15.000000             20.000000               2.000000   \n",
      "75%        28.461538             52.500000               2.000000   \n",
      "max       390.000000           7618.227273             750.000000   \n",
      "\n",
      "       purchase_count    total_price  \n",
      "count   197376.000000  197376.000000  \n",
      "mean         4.637448     130.336075  \n",
      "std         96.228665    2791.899946  \n",
      "min          0.000000  -36124.000000  \n",
      "25%          0.000000       0.000000  \n",
      "50%          1.000000       0.000000  \n",
      "75%          2.000000      75.000000  \n",
      "max      40272.000000  702080.290000  \n",
      "Statistiques Descriptives company 9\n",
      "       average_price  average_price_basket  average_ticket_basket  \\\n",
      "count  102710.000000         102710.000000          102710.000000   \n",
      "mean       60.312171             62.384177               1.042402   \n",
      "std        50.018101             52.009984               0.268064   \n",
      "min      -291.670000           -291.670000               1.000000   \n",
      "25%        41.500000             42.350000               1.000000   \n",
      "50%        59.000000             61.070000               1.000000   \n",
      "75%        74.550000             77.710000               1.000000   \n",
      "max      1116.500000           1216.950000              23.000000   \n",
      "\n",
      "       purchase_count    total_price  \n",
      "count   181134.000000  181134.000000  \n",
      "mean         1.021354      63.476966  \n",
      "std          1.805412     129.781944  \n",
      "min          0.000000    -291.670000  \n",
      "25%          0.000000       0.000000  \n",
      "50%          1.000000       0.000000  \n",
      "75%          1.000000      80.000000  \n",
      "max        273.000000   14343.950000  \n"
     ]
    }
   ],
   "source": [
    "for company in sport_comp:\n",
    "    print(f'Statistiques Descriptives company {company}')\n",
    "    company_data = customer_sport[customer_sport['number_company'] == company][['average_price', 'average_price_basket',\n",
    "          'average_ticket_basket', 'purchase_count', 'total_price']]\n",
    "    print(company_data.describe())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5058d3c9-73a0-4e01-881e-4d2423f0d291",
   "metadata": {},
   "outputs": [],
   "source": [
    "customer_sport[\"already_purchased\"] = customer_sport[\"purchase_count\"] > 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "848963c9-6129-4106-80b5-76bf814b70d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def mailing_consent(customer_sport):\n",
    "    df_graph = customer_sport.groupby([\"number_company\", \"already_purchased\"])[\"opt_in\"].mean().reset_index()\n",
    "    # Création du barplot groupé\n",
    "    fig, ax = plt.subplots(figsize=(10, 6))\n",
    "    \n",
    "    categories = df_graph[\"number_company\"].unique()\n",
    "    bar_width = 0.35\n",
    "    bar_positions = np.arange(len(categories))\n",
    "    \n",
    "    # Grouper les données par label et créer les barres groupées\n",
    "    for label in df_graph[\"already_purchased\"].unique():\n",
    "        label_data = df_graph[df_graph['already_purchased'] == label]\n",
    "        values = [label_data[label_data['number_company'] == category]['opt_in'].values[0]*100 for category in categories]\n",
    "    \n",
    "        label_printed = \"purchased\" if label else \"no purchase\"\n",
    "        ax.bar(bar_positions, values, bar_width, label=label_printed)\n",
    "    \n",
    "        # Mise à jour des positions des barres pour le prochain groupe\n",
    "        bar_positions = [pos + bar_width for pos in bar_positions]\n",
    "    \n",
    "    # Ajout des étiquettes, de la légende, etc.\n",
    "    ax.set_xlabel('Numero de compagnie')\n",
    "    ax.set_ylabel('Part de consentement (%)')\n",
    "    ax.set_title('Part de consentement au mailing selon les compagnies')\n",
    "    ax.set_xticks([pos + bar_width / 2 for pos in np.arange(len(categories))])\n",
    "    ax.set_xticklabels(categories)\n",
    "    ax.legend()\n",
    "    \n",
    "    # Affichage du plot\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b78ef715-c645-4625-a128-4f5b49e5339d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 34252 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "mailing_consent(customer_sport)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d8071891-e6f5-4d93-b039-9e99c20ec4b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def gender_bar(customer_sport):\n",
    "    company_genders = customer_sport.groupby(\"number_company\")[[\"gender_male\", \"gender_female\", \"gender_other\"]].mean().reset_index()\n",
    "    \n",
    "    # Création du barplot\n",
    "    plt.bar(company_genders[\"number_company\"], company_genders[\"gender_male\"], label = \"Homme\")\n",
    "    plt.bar(company_genders[\"number_company\"], company_genders[\"gender_female\"], \n",
    "            bottom = company_genders[\"gender_male\"], label = \"Femme\")\n",
    "    plt.bar(company_genders[\"number_company\"], company_genders[\"gender_other\"], \n",
    "            bottom = company_genders[\"gender_male\"] + company_genders[\"gender_female\"], label = \"Inconnu\")\n",
    "    \n",
    "    # Ajout de titres et d'étiquettes\n",
    "    plt.xlabel('Company')\n",
    "    plt.ylabel(\"Part de clients de chaque sexe\")\n",
    "    plt.title(\"Sexe des clients de chaque compagnie de musee\")\n",
    "    plt.legend()\n",
    "\n",
    "    # Définir les étiquettes de l'axe x\n",
    "    plt.xticks(company_genders[\"number_company\"], [\"{}\".format(i) for i in company_genders[\"number_company\"]])\n",
    " \n",
    "    \n",
    "    # Affichage du barplot\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "2fc30f1d-cf64-4efb-9442-4d97bb50b29f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 29292 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "gender_bar(customer_sport)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "4b3bb641-814b-4679-9a67-4eca87a920a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def country_bar(customer_sport):\n",
    "    company_country_fr = customer_sport.groupby(\"number_company\")[\"country_fr\"].mean().reset_index()\n",
    "    # Création du barplot\n",
    "    plt.bar(company_country_fr[\"number_company\"], company_country_fr[\"country_fr\"])\n",
    "    \n",
    "    # Ajout de titres et d'étiquettes\n",
    "    plt.xlabel('Company')\n",
    "    plt.ylabel(\"Part de clients français\")\n",
    "    plt.title(\"Nationalité des clients de chaque compagnie de sport\")\n",
    "    \n",
    "    # Affichage du barplot\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "01258674-6b98-49e4-93f4-f4185964999f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 24596 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "country_bar(customer_sport)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "1336c230-2e02-4559-90ac-a43bbb65b1c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['customer_id', 'street_id', 'structure_id', 'mcp_contact_id',\n",
       "       'fidelity', 'tenant_id', 'is_partner', 'deleted_at', 'gender',\n",
       "       'is_email_true', 'opt_in', 'last_buying_date', 'max_price',\n",
       "       'ticket_sum', 'average_price', 'average_purchase_delay',\n",
       "       'average_price_basket', 'average_ticket_basket', 'total_price',\n",
       "       'purchase_count', 'first_buying_date', 'country', 'gender_label',\n",
       "       'gender_female', 'gender_male', 'gender_other', 'country_fr',\n",
       "       'number_company', 'already_purchased'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customer_sport.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43d63ea3-75f4-4356-a7e9-35905d86baa5",
   "metadata": {},
   "source": [
    "### 2. campaigns_information"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "8d116e34-cdd6-4ef9-8622-474da79f79ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nombre de lignes de la table :  463098\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "customer_id                 0\n",
       "nb_campaigns                0\n",
       "nb_campaigns_opened         0\n",
       "time_to_open           178826\n",
       "number_company              0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"Nombre de lignes de la table : \",campaigns_sport.shape[0])\n",
    "campaigns_sport.isna().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "724d3c33-c219-4212-b8b6-dd78481674cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "def lazy_customer_plot(campaigns_sport_kpi):\n",
    "    company_lazy_customers = campaigns_sport_kpi.groupby(\"number_company\")[\"no_campaign_opened\"].mean().reset_index()\n",
    "    # Création du barplot\n",
    "    plt.bar(company_lazy_customers[\"number_company\"], company_lazy_customers[\"no_campaign_opened\"])\n",
    "    \n",
    "    # Ajout de titres et d'étiquettes\n",
    "    plt.xlabel('Company')\n",
    "    plt.ylabel(\"Part de clients n'ayant ouvert aucun mail\")\n",
    "    plt.title(\"Part de clients n'ayant ouvert aucun mail pour les compagnies de sport\")\n",
    "    \n",
    "    # Affichage du barplot\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e513f308-3a9c-40ed-99d5-ed420bd67384",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30432 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "lazy_customer_plot(campaigns_sport_kpi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "1b7ac0f0-903e-45ae-8f44-dc37ed36eafc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def campaigns_effectiveness(customer_sport, Train=False):\n",
    "    if not Train:\n",
    "        customer_sport[\"already_purchased\"] = customer_sport[\"purchase_count\"]>0\n",
    "\n",
    "    nb_customers_purchasing = customer_sport[customer_sport[\"already_purchased\"]].groupby([\"number_company\",\"already_purchased\"])[\"customer_id\"].count().reset_index()\n",
    "    nb_customers_no_purchase = customer_sport[~customer_sport[\"already_purchased\"]].groupby([\"number_company\",\"already_purchased\"])[\"customer_id\"].count().reset_index()\n",
    "\n",
    "    plt.bar(nb_customers_purchasing[\"number_company\"], nb_customers_purchasing[\"customer_id\"]/1000, label = \"has purchased\")\n",
    "    plt.bar(nb_customers_no_purchase[\"number_company\"], nb_customers_no_purchase[\"customer_id\"]/1000, \n",
    "            bottom = nb_customers_purchasing[\"customer_id\"]/1000, label = \"has not purchased\")\n",
    "    \n",
    "    \n",
    "    # Ajout de titres et d'étiquettes\n",
    "    plt.xlabel('Company')\n",
    "    plt.ylabel(\"Nombre de clients (en milliers)\")\n",
    "    plt.title(\"Nombre de clients ayant acheté ou été ciblés par des mails pour les compagnies de sport\")\n",
    "    plt.legend()\n",
    "    \n",
    "    # Affichage du barplot\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "3e05edab-fb8a-423b-b0ae-94e36eeeb3cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 36444 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "campaigns_effectiveness(customer_sport)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d08698b-e3ab-4038-ad26-990297520d43",
   "metadata": {},
   "source": [
    "## Evolution des Commandes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "93fd7b09-690d-490f-8a59-01be25da7445",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['ticket_id', 'customer_id', 'purchase_id', 'event_type_id',\n",
       "       'supplier_name', 'purchase_date', 'amount', 'is_full_price',\n",
       "       'name_event_types', 'name_facilities', 'name_categories', 'name_events',\n",
       "       'name_seasons', 'start_date_time', 'end_date_time', 'open'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "products_sport.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "2f5e32e1-224f-4cc4-a5c3-c4d5857df83c",
   "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>customer_id</th>\n",
       "      <th>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "      <th>time_to_open</th>\n",
       "      <th>number_company</th>\n",
       "      <th>no_campaign_opened</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5_160516</td>\n",
       "      <td>26</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0 days 01:30:27</td>\n",
       "      <td>5</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5_160517</td>\n",
       "      <td>73</td>\n",
       "      <td>49.0</td>\n",
       "      <td>2 days 01:30:16.909090909</td>\n",
       "      <td>5</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5_160518</td>\n",
       "      <td>25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaT</td>\n",
       "      <td>5</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5_160519</td>\n",
       "      <td>46</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0 days 09:31:47.250000</td>\n",
       "      <td>5</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5_160520</td>\n",
       "      <td>35</td>\n",
       "      <td>9.0</td>\n",
       "      <td>1 days 14:34:51.571428571</td>\n",
       "      <td>5</td>\n",
       "      <td>False</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",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>463093</th>\n",
       "      <td>9_1720340</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaT</td>\n",
       "      <td>9</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>463094</th>\n",
       "      <td>9_1720352</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0 days 08:30:32</td>\n",
       "      <td>9</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>463095</th>\n",
       "      <td>9_1720353</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaT</td>\n",
       "      <td>9</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>463096</th>\n",
       "      <td>9_1720354</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0 days 00:00:05</td>\n",
       "      <td>9</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>463097</th>\n",
       "      <td>9_1720355</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0 days 00:19:39</td>\n",
       "      <td>9</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>463098 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       customer_id  nb_campaigns  nb_campaigns_opened  \\\n",
       "0         5_160516            26                  2.0   \n",
       "1         5_160517            73                 49.0   \n",
       "2         5_160518            25                  0.0   \n",
       "3         5_160519            46                  5.0   \n",
       "4         5_160520            35                  9.0   \n",
       "...            ...           ...                  ...   \n",
       "463093   9_1720340             1                  0.0   \n",
       "463094   9_1720352             1                  1.0   \n",
       "463095   9_1720353             1                  0.0   \n",
       "463096   9_1720354             1                  1.0   \n",
       "463097   9_1720355             1                  1.0   \n",
       "\n",
       "                    time_to_open  number_company  no_campaign_opened  \n",
       "0                0 days 01:30:27               5               False  \n",
       "1      2 days 01:30:16.909090909               5               False  \n",
       "2                            NaT               5                True  \n",
       "3         0 days 09:31:47.250000               5               False  \n",
       "4      1 days 14:34:51.571428571               5               False  \n",
       "...                          ...             ...                 ...  \n",
       "463093                       NaT               9                True  \n",
       "463094           0 days 08:30:32               9               False  \n",
       "463095                       NaT               9                True  \n",
       "463096           0 days 00:00:05               9               False  \n",
       "463097           0 days 00:19:39               9               False  \n",
       "\n",
       "[463098 rows x 6 columns]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "campaigns_sport"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "b917f58e-fb8c-485b-808c-a53c04745833",
   "metadata": {},
   "outputs": [],
   "source": [
    "def sale_dynamics(products_sport, campaigns_sport_brut):\n",
    "    # Mois du premier achat\n",
    "    purchase_min = products_sport.groupby(['customer_id'])['purchase_date'].min().reset_index()\n",
    "    purchase_min.rename(columns = {'purchase_date' : 'first_purchase_event'}, inplace = True)\n",
    "    purchase_min['first_purchase_event'] = pd.to_datetime(purchase_min['first_purchase_event'])\n",
    "    purchase_min['first_purchase_month'] = pd.to_datetime(purchase_min['first_purchase_event'].dt.strftime('%Y-%m'))\n",
    "\n",
    "    # Mois du premier mails\n",
    "    first_mail_received = campaigns_sport_brut.groupby('customer_id')['sent_at'].min().reset_index()\n",
    "    first_mail_received.rename(columns = {'sent_at' : 'first_email_reception'}, inplace = True)\n",
    "    first_mail_received['first_email_reception'] = pd.to_datetime(first_mail_received['first_email_reception'])\n",
    "    first_mail_received['first_email_month'] = pd.to_datetime(first_mail_received['first_email_reception'].dt.strftime('%Y-%m'))\n",
    "\n",
    "    # Fusion \n",
    "    known_customer = pd.merge(purchase_min[['customer_id', 'first_purchase_month']], \n",
    "                          first_mail_received[['customer_id', 'first_email_month']], on = 'customer_id', how = 'outer')\n",
    "\n",
    "    # Mois à partir duquel le client est considere comme connu\n",
    "\n",
    "    known_customer['known_date'] = pd.to_datetime(known_customer[['first_email_month', 'first_purchase_month']].min(axis = 1), utc = True, format = 'ISO8601')\n",
    "\n",
    "    # Nombre de commande par mois\n",
    "    purchases_count = pd.merge(products_sport[['customer_id', 'purchase_id', 'purchase_date']].drop_duplicates(), known_customer[['customer_id', 'known_date']], on = ['customer_id'], how = 'inner')\n",
    "    purchases_count['is_customer_known'] = purchases_count['purchase_date'] > purchases_count['known_date'] + pd.DateOffset(months=1)\n",
    "    purchases_count['purchase_date_month'] = pd.to_datetime(purchases_count['purchase_date'].dt.strftime('%Y-%m'))\n",
    "    purchases_count = purchases_count[purchases_count['customer_id'] != 1]\n",
    "    \n",
    "    # Nombre de commande par mois par type de client\n",
    "    nb_purchases_graph = purchases_count.groupby(['purchase_date_month', 'is_customer_known'])['purchase_id'].count().reset_index()\n",
    "    nb_purchases_graph.rename(columns = {'purchase_id' : 'nb_purchases'}, inplace = True)\n",
    "    \n",
    "    nb_purchases_graph_2 = purchases_count.groupby(['purchase_date_month', 'is_customer_known'])['customer_id'].nunique().reset_index()\n",
    "    nb_purchases_graph_2.rename(columns = {'customer_id' : 'nb_new_customer'}, inplace = True)\n",
    "\n",
    "    # Graphique en nombre de commande\n",
    "    purchases_graph = nb_purchases_graph\n",
    "    \n",
    "    purchases_graph_used = purchases_graph[purchases_graph[\"purchase_date_month\"] >= datetime(2021,3,1)]\n",
    "    purchases_graph_used_0 = purchases_graph_used[purchases_graph_used[\"is_customer_known\"]==False]\n",
    "    purchases_graph_used_1 = purchases_graph_used[purchases_graph_used[\"is_customer_known\"]==True]\n",
    "    \n",
    "    \n",
    "    # Création du barplot\n",
    "    plt.bar(purchases_graph_used_0[\"purchase_date_month\"], purchases_graph_used_0[\"nb_purchases\"], width=12, label = \"Nouveau client\")\n",
    "    plt.bar(purchases_graph_used_0[\"purchase_date_month\"], purchases_graph_used_1[\"nb_purchases\"], \n",
    "            bottom = purchases_graph_used_0[\"nb_purchases\"], width=12, label = \"Ancien client\")\n",
    "    \n",
    "    \n",
    "    # commande pr afficher slt\n",
    "    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b%y'))\n",
    "    \n",
    "    \n",
    "    # Ajout de titres et d'étiquettes\n",
    "    plt.xlabel('Mois')\n",
    "    plt.ylabel(\"Nombre d'achats\")\n",
    "    plt.title(\"Nombre d'achats - Sport\")\n",
    "    plt.legend()\n",
    "    \n",
    "    # Affichage du barplot\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "7f0275ec-5cc5-436c-8d50-5263fd8a6945",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 31892 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sale_dynamics(products_sport, campaigns_sport_brut)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23b35899-728c-4674-bbbc-157643c16abe",
   "metadata": {},
   "source": [
    "# 3 - Caractéristiques Démographiques"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "b1bb86c5-3f40-4d5c-bef0-d6e8693c6b5e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bdc2324-data/5/5customersplus.csv\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Index(['id', 'lastname', 'firstname', 'birthdate', 'email', 'street_id',\n",
       "       'created_at', 'updated_at', 'civility', 'is_partner', 'extra',\n",
       "       'deleted_at', 'reference', 'gender', 'is_email_true', 'extra_field',\n",
       "       'identifier', 'opt_in', 'structure_id', 'note', 'profession',\n",
       "       'language', 'mcp_contact_id', 'need_reload', 'last_buying_date',\n",
       "       'max_price', 'ticket_sum', 'average_price', 'fidelity',\n",
       "       'average_purchase_delay', 'average_price_basket',\n",
       "       'average_ticket_basket', 'total_price', 'preferred_category',\n",
       "       'preferred_supplier', 'preferred_formula', 'purchase_count',\n",
       "       'first_buying_date', 'last_visiting_date', 'zipcode', 'country', 'age',\n",
       "       'tenant_id'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "directory_path = '5'\n",
    "file_name = \"5customersplus.csv\"\n",
    "file_path = \"bdc2324-data\" + \"/\" + directory_path + \"/\" + file_name\n",
    "print(file_path)\n",
    "with fs.open(file_path, mode=\"rb\") as file_in:\n",
    "    customersplus = pd.read_csv(file_in, sep=\",\")\n",
    "    \n",
    "customersplus.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "8adba9cc-e257-4c57-8149-c9af48c12b6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_customer_brut_dataset(directory_path):\n",
    "    file_name = str(directory_path) + \"customersplus.csv\"\n",
    "    print(file_name)\n",
    "    file_path = \"bdc2324-data\" + \"/\" + str(directory_path) + \"/\" + file_name\n",
    "    print(file_path)\n",
    "    with fs.open(file_path, mode=\"rb\") as file_in:\n",
    "        customersplus = pd.read_csv(file_in, sep=\",\")\n",
    "    return customersplus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "4c8f511b-2740-4b8d-bd99-d0ecf7d64e74",
   "metadata": {},
   "outputs": [],
   "source": [
    "def percent_of_na(company, column):\n",
    "    df = load_customer_brut_dataset(company)\n",
    "    if column in df.columns:\n",
    "        na_percentage = df[column].isna().mean() * 100\n",
    "        non_na_percentage = 100 - na_percentage\n",
    "        \n",
    "        labels = ['Valeurs Manquantes', 'Non-Valeurs Manquantes']\n",
    "        sizes = [na_percentage, non_na_percentage]\n",
    "        colors = ['#ff9999','#66b3ff']\n",
    "        explode = (0.1, 0)\n",
    "    \n",
    "        plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', startangle=140)\n",
    "        plt.axis('equal') \n",
    "        plt.title('Pourcentage de Valeurs Manquantes : {}'.format(column))\n",
    "        #plt.show()\n",
    "    else:\n",
    "        print(f\"The column {column} doesn't exist for the company {company}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "1ca50118-a32d-4dda-8fdf-92443f0f5196",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n",
      "5customersplus.csv\n",
      "bdc2324-data/5/5customersplus.csv\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[100], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m company \u001b[38;5;129;01min\u001b[39;00m customer_sport[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnumber_company\u001b[39m\u001b[38;5;124m'\u001b[39m]:\n\u001b[0;32m----> 2\u001b[0m     \u001b[43mpercent_of_na\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcompany\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mprofession\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[0;32mIn[99], line 2\u001b[0m, in \u001b[0;36mpercent_of_na\u001b[0;34m(company, column)\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mpercent_of_na\u001b[39m(company, column):\n\u001b[0;32m----> 2\u001b[0m     df \u001b[38;5;241m=\u001b[39m \u001b[43mload_customer_brut_dataset\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcompany\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m      3\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m column \u001b[38;5;129;01min\u001b[39;00m df\u001b[38;5;241m.\u001b[39mcolumns:\n\u001b[1;32m      4\u001b[0m         na_percentage \u001b[38;5;241m=\u001b[39m df[column]\u001b[38;5;241m.\u001b[39misna()\u001b[38;5;241m.\u001b[39mmean() \u001b[38;5;241m*\u001b[39m \u001b[38;5;241m100\u001b[39m\n",
      "Cell \u001b[0;32mIn[95], line 7\u001b[0m, in \u001b[0;36mload_customer_brut_dataset\u001b[0;34m(directory_path)\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[38;5;28mprint\u001b[39m(file_path)\n\u001b[1;32m      6\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m fs\u001b[38;5;241m.\u001b[39mopen(file_path, mode\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrb\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m file_in:\n\u001b[0;32m----> 7\u001b[0m     customersplus \u001b[38;5;241m=\u001b[39m \u001b[43mpd\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread_csv\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile_in\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msep\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m,\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m      8\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m customersplus\n",
      "File \u001b[0;32m/opt/mamba/lib/python3.11/site-packages/pandas/io/parsers/readers.py:1024\u001b[0m, in \u001b[0;36mread_csv\u001b[0;34m(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, date_format, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options, dtype_backend)\u001b[0m\n\u001b[1;32m   1011\u001b[0m kwds_defaults \u001b[38;5;241m=\u001b[39m _refine_defaults_read(\n\u001b[1;32m   1012\u001b[0m     dialect,\n\u001b[1;32m   1013\u001b[0m     delimiter,\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m   1020\u001b[0m     dtype_backend\u001b[38;5;241m=\u001b[39mdtype_backend,\n\u001b[1;32m   1021\u001b[0m )\n\u001b[1;32m   1022\u001b[0m kwds\u001b[38;5;241m.\u001b[39mupdate(kwds_defaults)\n\u001b[0;32m-> 1024\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_read\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfilepath_or_buffer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkwds\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m/opt/mamba/lib/python3.11/site-packages/pandas/io/parsers/readers.py:624\u001b[0m, in \u001b[0;36m_read\u001b[0;34m(filepath_or_buffer, kwds)\u001b[0m\n\u001b[1;32m    621\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m parser\n\u001b[1;32m    623\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m parser:\n\u001b[0;32m--> 624\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mparser\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnrows\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m/opt/mamba/lib/python3.11/site-packages/pandas/io/parsers/readers.py:1921\u001b[0m, in \u001b[0;36mTextFileReader.read\u001b[0;34m(self, nrows)\u001b[0m\n\u001b[1;32m   1914\u001b[0m nrows \u001b[38;5;241m=\u001b[39m validate_integer(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnrows\u001b[39m\u001b[38;5;124m\"\u001b[39m, nrows)\n\u001b[1;32m   1915\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m   1916\u001b[0m     \u001b[38;5;66;03m# error: \"ParserBase\" has no attribute \"read\"\u001b[39;00m\n\u001b[1;32m   1917\u001b[0m     (\n\u001b[1;32m   1918\u001b[0m         index,\n\u001b[1;32m   1919\u001b[0m         columns,\n\u001b[1;32m   1920\u001b[0m         col_dict,\n\u001b[0;32m-> 1921\u001b[0m     ) \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_engine\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread\u001b[49m\u001b[43m(\u001b[49m\u001b[43m  \u001b[49m\u001b[38;5;66;43;03m# type: ignore[attr-defined]\u001b[39;49;00m\n\u001b[1;32m   1922\u001b[0m \u001b[43m        \u001b[49m\u001b[43mnrows\u001b[49m\n\u001b[1;32m   1923\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1924\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[1;32m   1925\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mclose()\n",
      "File \u001b[0;32m/opt/mamba/lib/python3.11/site-packages/pandas/io/parsers/c_parser_wrapper.py:234\u001b[0m, in \u001b[0;36mCParserWrapper.read\u001b[0;34m(self, nrows)\u001b[0m\n\u001b[1;32m    232\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m    233\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mlow_memory:\n\u001b[0;32m--> 234\u001b[0m         chunks \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_reader\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mread_low_memory\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnrows\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    235\u001b[0m         \u001b[38;5;66;03m# destructive to chunks\u001b[39;00m\n\u001b[1;32m    236\u001b[0m         data \u001b[38;5;241m=\u001b[39m _concatenate_chunks(chunks)\n",
      "File \u001b[0;32mparsers.pyx:838\u001b[0m, in \u001b[0;36mpandas._libs.parsers.TextReader.read_low_memory\u001b[0;34m()\u001b[0m\n",
      "File \u001b[0;32mparsers.pyx:921\u001b[0m, in \u001b[0;36mpandas._libs.parsers.TextReader._read_rows\u001b[0;34m()\u001b[0m\n",
      "File \u001b[0;32mparsers.pyx:1083\u001b[0m, in \u001b[0;36mpandas._libs.parsers.TextReader._convert_column_data\u001b[0;34m()\u001b[0m\n",
      "File \u001b[0;32mparsers.pyx:1456\u001b[0m, in \u001b[0;36mpandas._libs.parsers._maybe_upcast\u001b[0;34m()\u001b[0m\n",
      "File \u001b[0;32m/opt/mamba/lib/python3.11/site-packages/numpy/core/multiarray.py:1131\u001b[0m, in \u001b[0;36mputmask\u001b[0;34m(a, mask, values)\u001b[0m\n\u001b[1;32m   1082\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m   1083\u001b[0m \u001b[38;5;124;03m    copyto(dst, src, casting='same_kind', where=True)\u001b[39;00m\n\u001b[1;32m   1084\u001b[0m \n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m   1126\u001b[0m \n\u001b[1;32m   1127\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[1;32m   1128\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m (dst, src, where)\n\u001b[0;32m-> 1131\u001b[0m \u001b[38;5;129m@array_function_from_c_func_and_dispatcher\u001b[39m(_multiarray_umath\u001b[38;5;241m.\u001b[39mputmask)\n\u001b[1;32m   1132\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mputmask\u001b[39m(a, \u001b[38;5;241m/\u001b[39m, mask, values):\n\u001b[1;32m   1133\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m   1134\u001b[0m \u001b[38;5;124;03m    putmask(a, mask, values)\u001b[39;00m\n\u001b[1;32m   1135\u001b[0m \n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m   1171\u001b[0m \n\u001b[1;32m   1172\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[1;32m   1173\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m (a, mask, values)\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 21404 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for company in customer_sport['number_company']:\n",
    "    percent_of_na(company, 'profession')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97326d89-d6f9-4e8f-9395-5c81def3831a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
