{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "be628bfc-0bca-48b0-97c9-29063289127e",
   "metadata": {},
   "source": [
    "# Statistiques descriptives : compagnies offrant des spectacles"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bf5450b-f44d-430a-aed7-d875dc365048",
   "metadata": {},
   "source": [
    "## Importations et chargement des données"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "aa915888-cede-4eb0-8a26-7df573d29a3e",
   "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 re\n",
    "import io"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "17949e81-c30b-4fdf-9872-d7dc2b22ba9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import KPI construction functions\n",
    "#exec(open('0_KPI_functions.py').read())\n",
    "exec(open('../0_KPI_functions.py').read())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9c1737a2-bad8-4266-8dec-452085d8cfe7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['projet-bdc2324-team1/0_Input/Company_10/campaigns_information.csv',\n",
       " 'projet-bdc2324-team1/0_Input/Company_10/customerplus_cleaned.csv',\n",
       " 'projet-bdc2324-team1/0_Input/Company_10/products_purchased_reduced.csv',\n",
       " 'projet-bdc2324-team1/0_Input/Company_10/target_information.csv']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "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})\n",
    "\n",
    "BUCKET = \"projet-bdc2324-team1/0_Input/Company_10\"\n",
    "fs.ls(BUCKET)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a35dc2f6-2017-4b21-abd2-2c4c112c96b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# test avec company 10\n",
    "\n",
    "dic_base=['campaigns_information','customerplus_cleaned','products_purchased_reduced','target_information']\n",
    "for nom_base in dic_base:\n",
    "    FILE_PATH_S3_fanta = 'projet-bdc2324-team1/0_Input/Company_10/' + nom_base + '.csv'\n",
    "    with fs.open(FILE_PATH_S3_fanta, mode=\"rb\") as file_in:\n",
    "        globals()[nom_base] = pd.read_csv(file_in, sep=\",\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "40b705eb-fd18-436b-b150-61611a3c6a84",
   "metadata": {},
   "outputs": [],
   "source": [
    "# fonction permettant d'extraire une table à partir du numéro de la compagnie (directory_path)\n",
    "\n",
    "def display_databases(directory_path, file_name, datetime_col = None):\n",
    "    \"\"\"\n",
    "    This function returns the file from s3 storage \n",
    "    \"\"\"\n",
    "    file_path = \"projet-bdc2324-team1\" + \"/0_Input/Company_\" + directory_path + \"/\" + file_name + \".csv\"\n",
    "    print(\"File path : \", file_path)\n",
    "    with fs.open(file_path, mode=\"rb\") as file_in:\n",
    "        df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)        \n",
    "    return df \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c56decc3-de19-4786-82a4-1386c72a6bfb",
   "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>id</th>\n",
       "      <th>customer_id</th>\n",
       "      <th>target_name</th>\n",
       "      <th>target_type_is_import</th>\n",
       "      <th>target_type_name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1165098</td>\n",
       "      <td>618562</td>\n",
       "      <td>Newsletter mensuelle</td>\n",
       "      <td>False</td>\n",
       "      <td>manual_static_filter</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1165100</td>\n",
       "      <td>618559</td>\n",
       "      <td>Newsletter mensuelle</td>\n",
       "      <td>False</td>\n",
       "      <td>manual_static_filter</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1165101</td>\n",
       "      <td>618561</td>\n",
       "      <td>Newsletter mensuelle</td>\n",
       "      <td>False</td>\n",
       "      <td>manual_static_filter</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1165102</td>\n",
       "      <td>618560</td>\n",
       "      <td>Newsletter mensuelle</td>\n",
       "      <td>False</td>\n",
       "      <td>manual_static_filter</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1165103</td>\n",
       "      <td>618558</td>\n",
       "      <td>Newsletter mensuelle</td>\n",
       "      <td>False</td>\n",
       "      <td>manual_static_filter</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>69253</th>\n",
       "      <td>1698158</td>\n",
       "      <td>18580</td>\n",
       "      <td>Newsletter mensuelle</td>\n",
       "      <td>False</td>\n",
       "      <td>manual_static_filter</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>69254</th>\n",
       "      <td>1698159</td>\n",
       "      <td>18569</td>\n",
       "      <td>Newsletter mensuelle</td>\n",
       "      <td>False</td>\n",
       "      <td>manual_static_filter</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>69255</th>\n",
       "      <td>1698160</td>\n",
       "      <td>2962</td>\n",
       "      <td>Newsletter mensuelle</td>\n",
       "      <td>False</td>\n",
       "      <td>manual_static_filter</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>69256</th>\n",
       "      <td>1698161</td>\n",
       "      <td>3825</td>\n",
       "      <td>Newsletter mensuelle</td>\n",
       "      <td>False</td>\n",
       "      <td>manual_static_filter</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>69257</th>\n",
       "      <td>1698162</td>\n",
       "      <td>5731</td>\n",
       "      <td>Newsletter mensuelle</td>\n",
       "      <td>False</td>\n",
       "      <td>manual_static_filter</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>69258 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            id  customer_id           target_name  target_type_is_import  \\\n",
       "0      1165098       618562  Newsletter mensuelle                  False   \n",
       "1      1165100       618559  Newsletter mensuelle                  False   \n",
       "2      1165101       618561  Newsletter mensuelle                  False   \n",
       "3      1165102       618560  Newsletter mensuelle                  False   \n",
       "4      1165103       618558  Newsletter mensuelle                  False   \n",
       "...        ...          ...                   ...                    ...   \n",
       "69253  1698158        18580  Newsletter mensuelle                  False   \n",
       "69254  1698159        18569  Newsletter mensuelle                  False   \n",
       "69255  1698160         2962  Newsletter mensuelle                  False   \n",
       "69256  1698161         3825  Newsletter mensuelle                  False   \n",
       "69257  1698162         5731  Newsletter mensuelle                  False   \n",
       "\n",
       "           target_type_name  \n",
       "0      manual_static_filter  \n",
       "1      manual_static_filter  \n",
       "2      manual_static_filter  \n",
       "3      manual_static_filter  \n",
       "4      manual_static_filter  \n",
       "...                     ...  \n",
       "69253  manual_static_filter  \n",
       "69254  manual_static_filter  \n",
       "69255  manual_static_filter  \n",
       "69256  manual_static_filter  \n",
       "69257  manual_static_filter  \n",
       "\n",
       "[69258 rows x 5 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "target_information"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c825d64b-356c-4b71-aa3c-90e0dd7ca092",
   "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>ticket_id</th>\n",
       "      <th>customer_id</th>\n",
       "      <th>purchase_id</th>\n",
       "      <th>event_type_id</th>\n",
       "      <th>supplier_name</th>\n",
       "      <th>purchase_date</th>\n",
       "      <th>amount</th>\n",
       "      <th>is_full_price</th>\n",
       "      <th>name_event_types</th>\n",
       "      <th>name_facilities</th>\n",
       "      <th>name_categories</th>\n",
       "      <th>name_events</th>\n",
       "      <th>name_seasons</th>\n",
       "      <th>start_date_time</th>\n",
       "      <th>end_date_time</th>\n",
       "      <th>open</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1799177</td>\n",
       "      <td>36984</td>\n",
       "      <td>409613</td>\n",
       "      <td>2</td>\n",
       "      <td>guichet</td>\n",
       "      <td>2016-04-28 17:58:26+02:00</td>\n",
       "      <td>9.0</td>\n",
       "      <td>False</td>\n",
       "      <td>danse</td>\n",
       "      <td>le grand t</td>\n",
       "      <td>abo t gourmand jeune</td>\n",
       "      <td>aringa rossa</td>\n",
       "      <td>test 2016/2017</td>\n",
       "      <td>2016-09-27 00:00:00+02:00</td>\n",
       "      <td>1901-01-01 00:09:21+00:09</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1799178</td>\n",
       "      <td>36984</td>\n",
       "      <td>409613</td>\n",
       "      <td>3</td>\n",
       "      <td>guichet</td>\n",
       "      <td>2016-04-28 17:58:26+02:00</td>\n",
       "      <td>9.0</td>\n",
       "      <td>False</td>\n",
       "      <td>cirque</td>\n",
       "      <td>le grand t</td>\n",
       "      <td>abo t gourmand jeune</td>\n",
       "      <td>5èmes hurlants</td>\n",
       "      <td>test 2016/2017</td>\n",
       "      <td>2016-11-18 00:00:00+01:00</td>\n",
       "      <td>1901-01-01 00:09:21+00:09</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1799179</td>\n",
       "      <td>36984</td>\n",
       "      <td>409613</td>\n",
       "      <td>1</td>\n",
       "      <td>guichet</td>\n",
       "      <td>2016-04-28 17:58:26+02:00</td>\n",
       "      <td>9.0</td>\n",
       "      <td>False</td>\n",
       "      <td>théâtre</td>\n",
       "      <td>le grand t</td>\n",
       "      <td>abo t gourmand jeune</td>\n",
       "      <td>dom juan</td>\n",
       "      <td>test 2016/2017</td>\n",
       "      <td>2016-12-07 00:00:00+01:00</td>\n",
       "      <td>1901-01-01 00:09:21+00:09</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1799180</td>\n",
       "      <td>36984</td>\n",
       "      <td>409613</td>\n",
       "      <td>1</td>\n",
       "      <td>guichet</td>\n",
       "      <td>2016-04-28 17:58:26+02:00</td>\n",
       "      <td>9.0</td>\n",
       "      <td>False</td>\n",
       "      <td>théâtre</td>\n",
       "      <td>le grand t</td>\n",
       "      <td>abo t gourmand jeune</td>\n",
       "      <td>vanishing point</td>\n",
       "      <td>test 2016/2017</td>\n",
       "      <td>2017-01-04 00:00:00+01:00</td>\n",
       "      <td>1901-01-01 00:09:21+00:09</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1799181</td>\n",
       "      <td>36984</td>\n",
       "      <td>409613</td>\n",
       "      <td>3</td>\n",
       "      <td>guichet</td>\n",
       "      <td>2016-04-28 17:58:26+02:00</td>\n",
       "      <td>12.0</td>\n",
       "      <td>False</td>\n",
       "      <td>cirque</td>\n",
       "      <td>la cite des congres</td>\n",
       "      <td>abo t gourmand jeune</td>\n",
       "      <td>a o lang pho</td>\n",
       "      <td>test 2016/2017</td>\n",
       "      <td>2017-01-03 00:00:00+01:00</td>\n",
       "      <td>1901-01-01 00:09:21+00:09</td>\n",
       "      <td>True</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",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>492309</th>\n",
       "      <td>3252232</td>\n",
       "      <td>621716</td>\n",
       "      <td>710062</td>\n",
       "      <td>1</td>\n",
       "      <td>guichet</td>\n",
       "      <td>2023-03-09 12:08:45+01:00</td>\n",
       "      <td>7.0</td>\n",
       "      <td>False</td>\n",
       "      <td>théâtre</td>\n",
       "      <td>cap nort</td>\n",
       "      <td>tarif sco co 1 seance scolaire</td>\n",
       "      <td>sur moi, le temps</td>\n",
       "      <td>2022/2023</td>\n",
       "      <td>2023-03-13 14:00:00+01:00</td>\n",
       "      <td>1901-01-01 00:09:21+00:09</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>492310</th>\n",
       "      <td>3252233</td>\n",
       "      <td>621716</td>\n",
       "      <td>710062</td>\n",
       "      <td>1</td>\n",
       "      <td>guichet</td>\n",
       "      <td>2023-03-09 12:08:45+01:00</td>\n",
       "      <td>7.0</td>\n",
       "      <td>False</td>\n",
       "      <td>théâtre</td>\n",
       "      <td>cap nort</td>\n",
       "      <td>tarif sco co 1 seance scolaire</td>\n",
       "      <td>sur moi, le temps</td>\n",
       "      <td>2022/2023</td>\n",
       "      <td>2023-03-13 14:00:00+01:00</td>\n",
       "      <td>1901-01-01 00:09:21+00:09</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>492311</th>\n",
       "      <td>3252234</td>\n",
       "      <td>621716</td>\n",
       "      <td>710062</td>\n",
       "      <td>1</td>\n",
       "      <td>guichet</td>\n",
       "      <td>2023-03-09 12:08:45+01:00</td>\n",
       "      <td>7.0</td>\n",
       "      <td>False</td>\n",
       "      <td>théâtre</td>\n",
       "      <td>cap nort</td>\n",
       "      <td>tarif sco co 1 seance scolaire</td>\n",
       "      <td>sur moi, le temps</td>\n",
       "      <td>2022/2023</td>\n",
       "      <td>2023-03-13 14:00:00+01:00</td>\n",
       "      <td>1901-01-01 00:09:21+00:09</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>492312</th>\n",
       "      <td>3252235</td>\n",
       "      <td>621716</td>\n",
       "      <td>710062</td>\n",
       "      <td>1</td>\n",
       "      <td>guichet</td>\n",
       "      <td>2023-03-09 12:08:45+01:00</td>\n",
       "      <td>7.0</td>\n",
       "      <td>False</td>\n",
       "      <td>théâtre</td>\n",
       "      <td>cap nort</td>\n",
       "      <td>tarif sco co 1 seance scolaire</td>\n",
       "      <td>sur moi, le temps</td>\n",
       "      <td>2022/2023</td>\n",
       "      <td>2023-03-13 14:00:00+01:00</td>\n",
       "      <td>1901-01-01 00:09:21+00:09</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>492313</th>\n",
       "      <td>3252236</td>\n",
       "      <td>621716</td>\n",
       "      <td>710062</td>\n",
       "      <td>1</td>\n",
       "      <td>guichet</td>\n",
       "      <td>2023-03-09 12:08:45+01:00</td>\n",
       "      <td>7.0</td>\n",
       "      <td>False</td>\n",
       "      <td>théâtre</td>\n",
       "      <td>cap nort</td>\n",
       "      <td>tarif sco co 1 seance scolaire</td>\n",
       "      <td>sur moi, le temps</td>\n",
       "      <td>2022/2023</td>\n",
       "      <td>2023-03-13 14:00:00+01:00</td>\n",
       "      <td>1901-01-01 00:09:21+00:09</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>492314 rows × 16 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        ticket_id  customer_id  purchase_id  event_type_id supplier_name  \\\n",
       "0         1799177        36984       409613              2       guichet   \n",
       "1         1799178        36984       409613              3       guichet   \n",
       "2         1799179        36984       409613              1       guichet   \n",
       "3         1799180        36984       409613              1       guichet   \n",
       "4         1799181        36984       409613              3       guichet   \n",
       "...           ...          ...          ...            ...           ...   \n",
       "492309    3252232       621716       710062              1       guichet   \n",
       "492310    3252233       621716       710062              1       guichet   \n",
       "492311    3252234       621716       710062              1       guichet   \n",
       "492312    3252235       621716       710062              1       guichet   \n",
       "492313    3252236       621716       710062              1       guichet   \n",
       "\n",
       "                    purchase_date  amount  is_full_price name_event_types  \\\n",
       "0       2016-04-28 17:58:26+02:00     9.0          False            danse   \n",
       "1       2016-04-28 17:58:26+02:00     9.0          False           cirque   \n",
       "2       2016-04-28 17:58:26+02:00     9.0          False          théâtre   \n",
       "3       2016-04-28 17:58:26+02:00     9.0          False          théâtre   \n",
       "4       2016-04-28 17:58:26+02:00    12.0          False           cirque   \n",
       "...                           ...     ...            ...              ...   \n",
       "492309  2023-03-09 12:08:45+01:00     7.0          False          théâtre   \n",
       "492310  2023-03-09 12:08:45+01:00     7.0          False          théâtre   \n",
       "492311  2023-03-09 12:08:45+01:00     7.0          False          théâtre   \n",
       "492312  2023-03-09 12:08:45+01:00     7.0          False          théâtre   \n",
       "492313  2023-03-09 12:08:45+01:00     7.0          False          théâtre   \n",
       "\n",
       "            name_facilities                 name_categories  \\\n",
       "0                le grand t            abo t gourmand jeune   \n",
       "1                le grand t            abo t gourmand jeune   \n",
       "2                le grand t            abo t gourmand jeune   \n",
       "3                le grand t            abo t gourmand jeune   \n",
       "4       la cite des congres            abo t gourmand jeune   \n",
       "...                     ...                             ...   \n",
       "492309             cap nort  tarif sco co 1 seance scolaire   \n",
       "492310             cap nort  tarif sco co 1 seance scolaire   \n",
       "492311             cap nort  tarif sco co 1 seance scolaire   \n",
       "492312             cap nort  tarif sco co 1 seance scolaire   \n",
       "492313             cap nort  tarif sco co 1 seance scolaire   \n",
       "\n",
       "              name_events    name_seasons            start_date_time  \\\n",
       "0            aringa rossa  test 2016/2017  2016-09-27 00:00:00+02:00   \n",
       "1          5èmes hurlants  test 2016/2017  2016-11-18 00:00:00+01:00   \n",
       "2                dom juan  test 2016/2017  2016-12-07 00:00:00+01:00   \n",
       "3         vanishing point  test 2016/2017  2017-01-04 00:00:00+01:00   \n",
       "4            a o lang pho  test 2016/2017  2017-01-03 00:00:00+01:00   \n",
       "...                   ...             ...                        ...   \n",
       "492309  sur moi, le temps       2022/2023  2023-03-13 14:00:00+01:00   \n",
       "492310  sur moi, le temps       2022/2023  2023-03-13 14:00:00+01:00   \n",
       "492311  sur moi, le temps       2022/2023  2023-03-13 14:00:00+01:00   \n",
       "492312  sur moi, le temps       2022/2023  2023-03-13 14:00:00+01:00   \n",
       "492313  sur moi, le temps       2022/2023  2023-03-13 14:00:00+01:00   \n",
       "\n",
       "                    end_date_time  open  \n",
       "0       1901-01-01 00:09:21+00:09  True  \n",
       "1       1901-01-01 00:09:21+00:09  True  \n",
       "2       1901-01-01 00:09:21+00:09  True  \n",
       "3       1901-01-01 00:09:21+00:09  True  \n",
       "4       1901-01-01 00:09:21+00:09  True  \n",
       "...                           ...   ...  \n",
       "492309  1901-01-01 00:09:21+00:09  True  \n",
       "492310  1901-01-01 00:09:21+00:09  True  \n",
       "492311  1901-01-01 00:09:21+00:09  True  \n",
       "492312  1901-01-01 00:09:21+00:09  True  \n",
       "492313  1901-01-01 00:09:21+00:09  True  \n",
       "\n",
       "[492314 rows x 16 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "products_purchased_reduced"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "afd044b8-ac83-4a35-b959-700cae0b3b41",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File path :  projet-bdc2324-team1/0_Input/Company_10/customerplus_cleaned.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File path :  projet-bdc2324-team1/0_Input/Company_10/campaigns_information.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File path :  projet-bdc2324-team1/0_Input/Company_10/products_purchased_reduced.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File path :  projet-bdc2324-team1/0_Input/Company_10/target_information.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n",
      "<string>:28: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\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"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tables imported for tenant 10\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_11/customerplus_cleaned.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File path :  projet-bdc2324-team1/0_Input/Company_11/campaigns_information.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File path :  projet-bdc2324-team1/0_Input/Company_11/products_purchased_reduced.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File path :  projet-bdc2324-team1/0_Input/Company_11/target_information.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n",
      "<string>:28: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\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"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tables imported for tenant 11\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_12/customerplus_cleaned.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File path :  projet-bdc2324-team1/0_Input/Company_12/campaigns_information.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File path :  projet-bdc2324-team1/0_Input/Company_12/products_purchased_reduced.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n",
      "/tmp/ipykernel_436/3170175140.py:10: DtypeWarning: Columns (4,8,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File path :  projet-bdc2324-team1/0_Input/Company_12/target_information.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n",
      "<string>:28: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\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"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tables imported for tenant 12\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_13/customerplus_cleaned.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File path :  projet-bdc2324-team1/0_Input/Company_13/campaigns_information.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File path :  projet-bdc2324-team1/0_Input/Company_13/products_purchased_reduced.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File path :  projet-bdc2324-team1/0_Input/Company_13/target_information.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n",
      "<string>:28: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\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"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tables imported for tenant 13\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_14/customerplus_cleaned.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File path :  projet-bdc2324-team1/0_Input/Company_14/campaigns_information.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File path :  projet-bdc2324-team1/0_Input/Company_14/products_purchased_reduced.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n",
      "/tmp/ipykernel_436/3170175140.py:10: DtypeWarning: Columns (8,9) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File path :  projet-bdc2324-team1/0_Input/Company_14/target_information.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n",
      "<string>:28: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\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"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tables imported for tenant 14\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=['10','11','12','13','14']\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_information = 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_compagny\"]=int(directory_path)\n",
    "    df_campaigns_kpi[\"number_compagny\"]=int(directory_path)\n",
    "    df_customerplus_clean[\"number_compagny\"]=int(directory_path)\n",
    "    df_target_information[\"number_compagny\"]=int(directory_path)\n",
    "\n",
    "    if nb_compagnie.index(directory_path)>=1:\n",
    "        customerplus_clean_spectacle=pd.concat([customerplus_clean_spectacle,df_customerplus_clean],axis=0)\n",
    "        campaigns_information_spectacle=pd.concat([campaigns_information_spectacle,df_campaigns_kpi],axis=0)\n",
    "        products_purchased_reduced_spectacle=pd.concat([products_purchased_reduced_spectacle,df_tickets_kpi],axis=0)\n",
    "        target_information_spectacle=pd.concat([target_information_spectacle,df_target_information],axis=0)\n",
    "    else:\n",
    "        customerplus_clean_spectacle=df_customerplus_clean\n",
    "        campaigns_information_spectacle=df_campaigns_kpi\n",
    "        products_purchased_reduced_spectacle=df_tickets_kpi\n",
    "        target_information_spectacle=df_target_information\n",
    "\n",
    "    print(f\"Tables imported for tenant {directory_path}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "b5a4a031-9533-4a50-8569-5f4246691a7a",
   "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>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>has_tags</th>\n",
       "      <th>number_compagny</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>2</td>\n",
       "      <td>139</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18031</th>\n",
       "      <td>2</td>\n",
       "      <td>319517</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>1556</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>2020-01-01 14:06:52+00:00</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>0</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>291642</th>\n",
       "      <td>2</td>\n",
       "      <td>757541</td>\n",
       "      <td>303.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "      <td>862</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>2016-09-08 14:50:00+00:00</td>\n",
       "      <td>fr</td>\n",
       "      <td>male</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 29 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        customer_id  street_id  structure_id  mcp_contact_id  fidelity  \\\n",
       "17                2        139           NaN             NaN         0   \n",
       "18031             2     319517           NaN             NaN         0   \n",
       "291642            2     757541         303.0             5.0         1   \n",
       "\n",
       "        tenant_id  is_partner  deleted_at  gender  is_email_true  ...  \\\n",
       "17            875       False         NaN       2          False  ...   \n",
       "18031        1556       False         NaN       0           True  ...   \n",
       "291642        862       False         NaN       1           True  ...   \n",
       "\n",
       "        purchase_count          first_buying_date  country  gender_label  \\\n",
       "17                   3                        NaN      NaN         other   \n",
       "18031                2  2020-01-01 14:06:52+00:00       fr        female   \n",
       "291642               3  2016-09-08 14:50:00+00:00       fr          male   \n",
       "\n",
       "        gender_female  gender_male  gender_other  country_fr  has_tags  \\\n",
       "17                  0            0             1         NaN         0   \n",
       "18031               1            0             0         1.0         0   \n",
       "291642              0            1             0         1.0         1   \n",
       "\n",
       "        number_compagny  \n",
       "17                   10  \n",
       "18031                11  \n",
       "291642               14  \n",
       "\n",
       "[3 rows x 29 columns]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customerplus_clean_spectacle[customerplus_clean_spectacle[\"customer_id\"]==2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b9b6ec1f-36fb-4ee9-a1ed-09ff41878005",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'customerplus_clean_spectacle' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mcustomerplus_clean_spectacle\u001b[49m[customerplus_clean_spectacle[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcustomer_id\u001b[39m\u001b[38;5;124m\"\u001b[39m]\u001b[38;5;241m==\u001b[39m\u001b[38;5;241m1\u001b[39m]\n",
      "\u001b[0;31mNameError\u001b[0m: name 'customerplus_clean_spectacle' is not defined"
     ]
    }
   ],
   "source": [
    "customerplus_clean_spectacle[customerplus_clean_spectacle[\"customer_id\"]==1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "a12c1b7d-6f6f-483e-b215-6336d7a51057",
   "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",
       "       'has_tags', 'number_compagny'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customerplus_clean_spectacle.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "05b9a396-dcd7-4d3d-8b39-5ca48beba4b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#customerplus_clean_spectacle.isna().sum()\n",
    "#campaigns_information_spectacle.isna().sum()\n",
    "#products_purchased_reduced_spectacle.isna().sum()\n",
    "#target_information_spectacle.isna().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81e15508-32ca-46f1-a03d-1febddbbf5b4",
   "metadata": {},
   "source": [
    "### Ajout : importation de la table train_set pour faire les stats desc dessus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "3a1fdd6b-ac43-4e90-9a31-4f522bcc44bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3450421856.py:9: DtypeWarning: Columns (38) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  train_set_spectacle = pd.read_csv(file_in, sep=\",\")\n"
     ]
    }
   ],
   "source": [
    "# importation de la table train_set pour les compagnies de spectacle (ou musique)\n",
    "\n",
    "S3_ENDPOINT_URL = \"https://\" + os.environ[\"AWS_S3_ENDPOINT\"]\n",
    "fs = s3fs.S3FileSystem(client_kwargs={'endpoint_url': S3_ENDPOINT_URL})\n",
    "\n",
    "path_train_set_spectacle = \"projet-bdc2324-team1/Generalization/musique/Train_set.csv\"\n",
    "\n",
    "with fs.open(path_train_set_spectacle, mode=\"rb\") as file_in:\n",
    "        train_set_spectacle = pd.read_csv(file_in, sep=\",\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "3a4c1ff4-2861-4e86-99df-26eea0370dc3",
   "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_tickets</th>\n",
       "      <th>nb_purchases</th>\n",
       "      <th>total_amount</th>\n",
       "      <th>nb_suppliers</th>\n",
       "      <th>vente_internet_max</th>\n",
       "      <th>purchase_date_min</th>\n",
       "      <th>purchase_date_max</th>\n",
       "      <th>time_between_purchase</th>\n",
       "      <th>nb_tickets_internet</th>\n",
       "      <th>...</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>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "      <th>time_to_open</th>\n",
       "      <th>y_has_purchased</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10_492779</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>550.0</td>\n",
       "      <td>550.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</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>13.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>8 days 04:08:27</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10_563424</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>550.0</td>\n",
       "      <td>550.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>fr</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>0 days 01:39:58.555555555</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10_44369</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>550.0</td>\n",
       "      <td>550.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>fr</td>\n",
       "      <td>male</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10_620271</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>550.0</td>\n",
       "      <td>550.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10_687644</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>550.0</td>\n",
       "      <td>550.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 40 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  customer_id  nb_tickets  nb_purchases  total_amount  nb_suppliers  \\\n",
       "0   10_492779         0.0           0.0           0.0           0.0   \n",
       "1   10_563424         0.0           0.0           0.0           0.0   \n",
       "2    10_44369         0.0           0.0           0.0           0.0   \n",
       "3   10_620271         0.0           0.0           0.0           0.0   \n",
       "4   10_687644         0.0           0.0           0.0           0.0   \n",
       "\n",
       "   vente_internet_max  purchase_date_min  purchase_date_max  \\\n",
       "0                 0.0              550.0              550.0   \n",
       "1                 0.0              550.0              550.0   \n",
       "2                 0.0              550.0              550.0   \n",
       "3                 0.0              550.0              550.0   \n",
       "4                 0.0              550.0              550.0   \n",
       "\n",
       "   time_between_purchase  nb_tickets_internet  ...  country  gender_label  \\\n",
       "0                   -1.0                  0.0  ...       fr        female   \n",
       "1                   -1.0                  0.0  ...       fr         other   \n",
       "2                   -1.0                  0.0  ...       fr          male   \n",
       "3                   -1.0                  0.0  ...      NaN         other   \n",
       "4                   -1.0                  0.0  ...      NaN         other   \n",
       "\n",
       "   gender_female  gender_male  gender_other  country_fr  nb_campaigns  \\\n",
       "0              1            0             0         1.0          13.0   \n",
       "1              0            0             1         1.0          10.0   \n",
       "2              0            1             0         1.0          14.0   \n",
       "3              0            0             1         NaN           9.0   \n",
       "4              0            0             1         NaN           4.0   \n",
       "\n",
       "   nb_campaigns_opened               time_to_open  y_has_purchased  \n",
       "0                  4.0            8 days 04:08:27              0.0  \n",
       "1                  9.0  0 days 01:39:58.555555555              0.0  \n",
       "2                  0.0                        NaN              0.0  \n",
       "3                  0.0                        NaN              0.0  \n",
       "4                  0.0                        NaN              0.0  \n",
       "\n",
       "[5 rows x 40 columns]"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_set_spectacle.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "4632384d-2a06-445d-9fdb-b0c91b37ebaf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 1.])"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# on remplace les valeurs has purchased = NaN par des 0\n",
    "train_set_spectacle[\"y_has_purchased\"] = train_set_spectacle[\"y_has_purchased\"].fillna(0)\n",
    "train_set_spectacle[\"y_has_purchased\"].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "5fd56696-b479-46c7-8a59-fb8137db5fb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10, 11, 12, 13, 14])"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# on reproduit une colonne avec le numéro de la compagnie \n",
    "\n",
    "train_set_spectacle[\"number_company\"] = train_set_spectacle[\"customer_id\"].apply(lambda x : int(re.split(\"_\", str(x))[0]))\n",
    "train_set_spectacle[\"number_company\"].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "91c6e047-43d2-456c-81f1-087026eef4f0",
   "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_tickets</th>\n",
       "      <th>nb_purchases</th>\n",
       "      <th>total_amount</th>\n",
       "      <th>nb_suppliers</th>\n",
       "      <th>vente_internet_max</th>\n",
       "      <th>purchase_date_min</th>\n",
       "      <th>purchase_date_max</th>\n",
       "      <th>time_between_purchase</th>\n",
       "      <th>nb_tickets_internet</th>\n",
       "      <th>...</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>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "      <th>time_to_open</th>\n",
       "      <th>y_has_purchased</th>\n",
       "      <th>number_company</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10_492779</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>550.0</td>\n",
       "      <td>550.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>female</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>8 days 04:08:27</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10_563424</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>550.0</td>\n",
       "      <td>550.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>0 days 01:39:58.555555555</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10_44369</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>550.0</td>\n",
       "      <td>550.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>male</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10_620271</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>550.0</td>\n",
       "      <td>550.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10_687644</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>550.0</td>\n",
       "      <td>550.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 41 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  customer_id  nb_tickets  nb_purchases  total_amount  nb_suppliers  \\\n",
       "0   10_492779         0.0           0.0           0.0           0.0   \n",
       "1   10_563424         0.0           0.0           0.0           0.0   \n",
       "2    10_44369         0.0           0.0           0.0           0.0   \n",
       "3   10_620271         0.0           0.0           0.0           0.0   \n",
       "4   10_687644         0.0           0.0           0.0           0.0   \n",
       "\n",
       "   vente_internet_max  purchase_date_min  purchase_date_max  \\\n",
       "0                 0.0              550.0              550.0   \n",
       "1                 0.0              550.0              550.0   \n",
       "2                 0.0              550.0              550.0   \n",
       "3                 0.0              550.0              550.0   \n",
       "4                 0.0              550.0              550.0   \n",
       "\n",
       "   time_between_purchase  nb_tickets_internet  ...  gender_label  \\\n",
       "0                   -1.0                  0.0  ...        female   \n",
       "1                   -1.0                  0.0  ...         other   \n",
       "2                   -1.0                  0.0  ...          male   \n",
       "3                   -1.0                  0.0  ...         other   \n",
       "4                   -1.0                  0.0  ...         other   \n",
       "\n",
       "   gender_female  gender_male  gender_other  country_fr  nb_campaigns  \\\n",
       "0              1            0             0         1.0          13.0   \n",
       "1              0            0             1         1.0          10.0   \n",
       "2              0            1             0         1.0          14.0   \n",
       "3              0            0             1         NaN           9.0   \n",
       "4              0            0             1         NaN           4.0   \n",
       "\n",
       "   nb_campaigns_opened               time_to_open  y_has_purchased  \\\n",
       "0                  4.0            8 days 04:08:27              0.0   \n",
       "1                  9.0  0 days 01:39:58.555555555              0.0   \n",
       "2                  0.0                        NaN              0.0   \n",
       "3                  0.0                        NaN              0.0   \n",
       "4                  0.0                        NaN              0.0   \n",
       "\n",
       "   number_company  \n",
       "0              10  \n",
       "1              10  \n",
       "2              10  \n",
       "3              10  \n",
       "4              10  \n",
       "\n",
       "[5 rows x 41 columns]"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_set_spectacle.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fff306c2-1d41-4ef6-867b-ba9a7cf4ee68",
   "metadata": {},
   "source": [
    "## Statistiques descriptives"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0549bdc4-edd7-4511-916e-26e94b5a30f5",
   "metadata": {},
   "source": [
    "### 0. Détection du client anonyme (outlier) - utile pour la section 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "5b460061-f8b5-4a6b-ba59-539446d8487f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def outlier_detection(directory_path = \"1\", coupure = 1):\n",
    "    df_tickets = display_databases(directory_path, file_name = 'products_purchased_reduced' , datetime_col = ['purchase_date'])\n",
    "    df_tickets_kpi = tickets_kpi_function(df_tickets)\n",
    "\n",
    "    if directory_path == \"101\" :\n",
    "        df_tickets_1 = display_databases(directory_path, file_name = 'products_purchased_reduced_1' , datetime_col = ['purchase_date'])\n",
    "        df_tickets_kpi_1 = tickets_kpi_function(df_tickets_1)\n",
    "\n",
    "        df_tickets_kpi = pd.concat([df_tickets_kpi, df_tickets_kpi_1])\n",
    "    # Part du CA par customer\n",
    "    total_amount_share = df_tickets_kpi.groupby('customer_id')['total_amount'].sum().reset_index()\n",
    "    total_amount_share['total_amount_entreprise'] = total_amount_share['total_amount'].sum()\n",
    "    total_amount_share['share_total_amount'] = total_amount_share['total_amount']/total_amount_share['total_amount_entreprise']\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",
    "    \n",
    "    top = df_circulaire[:coupure]\n",
    "    rest = df_circulaire[coupure:]\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",
    "    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('Répartition des montants totaux')\n",
    "    plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "id": "cccee90c-67d1-4e14-8410-1210a5ef97d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# def d'une fonction permettant de générer un barplot à plusieurs barres selon une modalité \n",
    "\n",
    "def multiple_barplot(data, x, y, var_labels, bar_width=0.35,\n",
    "                    figsize=(10, 6), xlabel=None, ylabel=None, title=None, dico_labels = None) :\n",
    "\n",
    "    # si on donne aucun nom pour la legende, le graphique reprend les noms des variables x et y \n",
    "    xlabel = x if xlabel==None else xlabel\n",
    "    ylabel = y if ylabel==None else ylabel\n",
    "    \n",
    "    fig, ax = plt.subplots(figsize=figsize)\n",
    "    \n",
    "    categories = data[x].unique()\n",
    "    bar_width = bar_width\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 data[var_labels].unique():\n",
    "        label_data = data[data[var_labels] == label]\n",
    "        values = [label_data[label_data[x] == category][y].values[0] for category in categories]\n",
    "    \n",
    "        # label_printed = \"achat durant la période\" if label else \"aucun achat\"\n",
    "        label_printed = f\"{var_labels}={label}\" if dico_labels==None else dico_labels[label]\n",
    "        \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(xlabel)\n",
    "    ax.set_ylabel(ylabel)\n",
    "    ax.set_title(title)\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 - la proportion de français est la même selon qu'il y ait achat sur la période ou non\n",
    "    # sauf compagnie 12, et peut-être 13\n",
    "    # plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "b6417f09-a6c7-4319-95b3-98c95ec5a3b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "File path :  projet-bdc2324-team1/0_Input/Company_10/products_purchased_reduced.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 19756 characters]",
      "text/plain": [
       "<Figure size 300x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# outlier à enlever (dépend des stats desc !)\n",
    "outlier_detection(directory_path=\"10\") # mettre 2 si on veut le 1er client non anonyme"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "f08c082e-f76f-41f3-9530-3e6700eb74d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "outlier for tenant 10\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_10/products_purchased_reduced.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 19756 characters]",
      "text/plain": [
       "<Figure size 300x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "outlier for tenant 11\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_11/products_purchased_reduced.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 18288 characters]",
      "text/plain": [
       "<Figure size 300x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "outlier for tenant 12\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_12/products_purchased_reduced.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n",
      "/tmp/ipykernel_436/3170175140.py:10: DtypeWarning: Columns (4,8,10) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 20040 characters]",
      "text/plain": [
       "<Figure size 300x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "outlier for tenant 13\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_13/products_purchased_reduced.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 19520 characters]",
      "text/plain": [
       "<Figure size 300x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "outlier for tenant 14\n",
      "File path :  projet-bdc2324-team1/0_Input/Company_14/products_purchased_reduced.csv\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_436/3170175140.py:10: FutureWarning: The argument 'date_parser' is deprecated and will be removed in a future version. Please use 'date_format' instead, or read your data in as 'object' dtype and then call 'to_datetime'.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n",
      "/tmp/ipykernel_436/3170175140.py:10: DtypeWarning: Columns (8,9) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  df = pd.read_csv(file_in, sep=\",\", parse_dates = datetime_col, date_parser=custom_date_parser)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 18876 characters]",
      "text/plain": [
       "<Figure size 300x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# boucle pour identifier les outliers de chaque compagnie (et le client principal non anonyme)\n",
    "\n",
    "# nb_compagnie=['10','11','12','13','14']\n",
    "for company_number in nb_compagnie :\n",
    "    print(f\"outlier for tenant {company_number}\")\n",
    "    outlier_detection(directory_path=company_number, coupure = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dbe1af6a-79e9-45c7-a810-c6df3bf647f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# print(products_purchased_reduced_spectacle.loc[products_purchased_reduced_spectacle[\"number_compagny\"]==10][\"total_amount\"].describe())\n",
    "\n",
    "products_purchased_reduced_spectacle.loc[(products_purchased_reduced_spectacle[\"number_compagny\"]==10) & \n",
    "(products_purchased_reduced_spectacle[\"customer_id\"]==19521)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20e2b8a2-f31c-42a4-8ea5-7ad67ab66915",
   "metadata": {},
   "outputs": [],
   "source": [
    "# company 11 \n",
    "# etrange, pas de vente sur internet, et un seul supplier. Plus de 9k achats\n",
    "products_purchased_reduced_spectacle.loc[(products_purchased_reduced_spectacle[\"number_compagny\"]==11) & \n",
    "(products_purchased_reduced_spectacle[\"customer_id\"]==36)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5dbce57c-d091-4ce2-92f9-1201deb2462e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# company 12\n",
    "products_purchased_reduced_spectacle.loc[(products_purchased_reduced_spectacle[\"number_compagny\"]==12) & \n",
    "(products_purchased_reduced_spectacle[\"customer_id\"]==1706757)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a243b57-19da-4e29-a53d-bb8d03e2ab77",
   "metadata": {},
   "outputs": [],
   "source": [
    "# company 13\n",
    "products_purchased_reduced_spectacle.loc[(products_purchased_reduced_spectacle[\"number_compagny\"]==13) & \n",
    "(products_purchased_reduced_spectacle[\"customer_id\"]==8422)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d9b01bc-9584-4882-bd06-7de8acb8a88f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# company 14\n",
    "# a-t-on vrmt un outlier ? A acheté quasi 3k tickets, pr 96 achats\n",
    "products_purchased_reduced_spectacle.loc[(products_purchased_reduced_spectacle[\"number_compagny\"]==14) & \n",
    "(products_purchased_reduced_spectacle[\"customer_id\"]==6354)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "033c1e00-52bd-4651-b893-57bda531760e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# verifs dans les tables customerplus (outlier incertain pr 11 et 14)\n",
    "\n",
    "customerplus_clean_spectacle.loc[(customerplus_clean_spectacle[\"customer_id\"]==36) &\n",
    "(customerplus_clean_spectacle[\"number_compagny\"]==11)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28ac8cda-32fa-4fb7-a75b-e1cc24871c39",
   "metadata": {},
   "outputs": [],
   "source": [
    "customerplus_clean_spectacle.loc[(customerplus_clean_spectacle[\"customer_id\"]==6354) &\n",
    "(customerplus_clean_spectacle[\"number_compagny\"]==14)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3faea297-2cc5-4704-af85-77d95f600cc1",
   "metadata": {},
   "outputs": [],
   "source": [
    "customerplus_clean_spectacle.loc[(customerplus_clean_spectacle[\"customer_id\"]==8422) &\n",
    "(customerplus_clean_spectacle[\"number_compagny\"]==13)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b165ea79-347b-46fb-8217-635d9e888c65",
   "metadata": {},
   "outputs": [],
   "source": [
    "customerplus_clean_spectacle.loc[(customerplus_clean_spectacle[\"customer_id\"]==19521) &\n",
    "(customerplus_clean_spectacle[\"number_compagny\"]==10)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "282b0a96-5e78-48aa-9c2c-7d00d3907add",
   "metadata": {},
   "outputs": [],
   "source": [
    "customerplus_clean_spectacle.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "ad47a812-a744-49c5-8079-0919b49ef24c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# on enlève les outliers des tables\n",
    "\n",
    "outliers_musique_dico = {10 : 19521, 11 : 36, 12 : 1706757, 13 : 8422}\n",
    "\n",
    "# outlier_music_list = list(outliers_musique_dico.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "9717dfd5-c39c-41eb-858d-5baf3ab71554",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 19521\n",
      "11 36\n",
      "12 1706757\n",
      "13 8422\n"
     ]
    }
   ],
   "source": [
    "for tenant_number, customer_id in outliers_musique_dico.items() :\n",
    "\n",
    "    print(tenant_number, customer_id)\n",
    "    \n",
    "    customerplus_clean_spectacle = customerplus_clean_spectacle[(customerplus_clean_spectacle['number_compagny']!= tenant_number) |\n",
    "    (customerplus_clean_spectacle['customer_id']!= customer_id) ]\n",
    "\n",
    "    campaigns_information_spectacle = campaigns_information_spectacle[(campaigns_information_spectacle['number_compagny']!= tenant_number) |\n",
    "    (campaigns_information_spectacle['customer_id']!= customer_id) ]\n",
    "\n",
    "    products_purchased_reduced_spectacle = products_purchased_reduced_spectacle[(products_purchased_reduced_spectacle['number_compagny']!= tenant_number) |\n",
    "    (products_purchased_reduced_spectacle['customer_id']!= customer_id) ]\n",
    "\n",
    "    target_information_spectacle = target_information_spectacle[(target_information_spectacle['number_compagny']!= tenant_number) |\n",
    "    (target_information_spectacle['customer_id']!= customer_id) ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb7f4c95-817b-4145-9319-11d2f62b24d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# on vérifie que les outliers sont pas dans le train set "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "b50e1de8-28fe-42bd-bd81-dde7e36b64fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['10_19521', '11_36', '12_1706757', '13_8422']"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "outliers_train_set_musique = [str(tenant_id) + \"_\" + str(customer_id) for tenant_id, customer_id in outliers_musique_dico.items()]\n",
    "outliers_train_set_musique"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "id": "1753d45d-beac-48a4-9bc4-f84925320a89",
   "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_tickets</th>\n",
       "      <th>nb_purchases</th>\n",
       "      <th>total_amount</th>\n",
       "      <th>nb_suppliers</th>\n",
       "      <th>vente_internet_max</th>\n",
       "      <th>purchase_date_min</th>\n",
       "      <th>purchase_date_max</th>\n",
       "      <th>time_between_purchase</th>\n",
       "      <th>nb_tickets_internet</th>\n",
       "      <th>...</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>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "      <th>time_to_open</th>\n",
       "      <th>y_has_purchased</th>\n",
       "      <th>number_company</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>0 rows × 41 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [customer_id, nb_tickets, nb_purchases, total_amount, nb_suppliers, vente_internet_max, purchase_date_min, purchase_date_max, time_between_purchase, nb_tickets_internet, street_id, structure_id, mcp_contact_id, fidelity, tenant_id, is_partner, deleted_at, gender, is_email_true, opt_in, last_buying_date, max_price, ticket_sum, average_price, average_purchase_delay, average_price_basket, average_ticket_basket, total_price, purchase_count, first_buying_date, country, gender_label, gender_female, gender_male, gender_other, country_fr, nb_campaigns, nb_campaigns_opened, time_to_open, y_has_purchased, number_company]\n",
       "Index: []\n",
       "\n",
       "[0 rows x 41 columns]"
      ]
     },
     "execution_count": 161,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_set_spectacle[train_set_spectacle[\"customer_id\"].isin(outliers_train_set_musique)] # OK"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42f8171c-e80d-4faa-b278-21fcbe3b242c",
   "metadata": {},
   "source": [
    "### 1. customerplus_clean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "47f98721-53dd-4f8f-85ac-88043ee8d967",
   "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_compagny</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>821538</td>\n",
       "      <td>139</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</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>NaN</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>809126</td>\n",
       "      <td>1063</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</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>fr</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>11005</td>\n",
       "      <td>1063</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14</td>\n",
       "      <td>NaN</td>\n",
       "      <td>fr</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>17663</td>\n",
       "      <td>12731</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</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>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>38100</td>\n",
       "      <td>12395</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</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>1</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>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>307036</td>\n",
       "      <td>139</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2946</td>\n",
       "      <td>1063</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8</td>\n",
       "      <td>NaN</td>\n",
       "      <td>fr</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>18441</td>\n",
       "      <td>11139</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>fr</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9231</td>\n",
       "      <td>139</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</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>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>female</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>9870</td>\n",
       "      <td>139</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10 rows × 28 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   customer_id  street_id  structure_id  mcp_contact_id  fidelity  tenant_id  \\\n",
       "0       821538        139           NaN             NaN         0        875   \n",
       "1       809126       1063           NaN             NaN         0        875   \n",
       "2        11005       1063           NaN             NaN         0        875   \n",
       "3        17663      12731           NaN             NaN         0        875   \n",
       "4        38100      12395           NaN             NaN         0        875   \n",
       "5       307036        139           NaN             NaN         0        875   \n",
       "6         2946       1063           NaN             NaN         0        875   \n",
       "7        18441      11139           NaN             NaN         0        875   \n",
       "8         9231        139           NaN             NaN         0        875   \n",
       "9         9870        139           NaN             NaN         0        875   \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          False  ...          NaN   \n",
       "3       False         NaN       0          False  ...          NaN   \n",
       "4       False         NaN       0           True  ...          NaN   \n",
       "5       False         NaN       2           True  ...          NaN   \n",
       "6       False         NaN       2          False  ...          NaN   \n",
       "7       False         NaN       2          False  ...          NaN   \n",
       "8       False         NaN       0           True  ...          NaN   \n",
       "9       False         NaN       2           True  ...          NaN   \n",
       "\n",
       "  purchase_count  first_buying_date  country  gender_label  gender_female  \\\n",
       "0              0                NaN      NaN         other              0   \n",
       "1              0                NaN       fr         other              0   \n",
       "2             14                NaN       fr         other              0   \n",
       "3              1                NaN       fr        female              1   \n",
       "4              1                NaN       fr        female              1   \n",
       "5              1                NaN      NaN         other              0   \n",
       "6              8                NaN       fr         other              0   \n",
       "7              3                NaN       fr         other              0   \n",
       "8              1                NaN      NaN        female              1   \n",
       "9              1                NaN      NaN         other              0   \n",
       "\n",
       "   gender_male  gender_other  country_fr  number_compagny  \n",
       "0            0             1         NaN               10  \n",
       "1            0             1         1.0               10  \n",
       "2            0             1         1.0               10  \n",
       "3            0             0         1.0               10  \n",
       "4            0             0         1.0               10  \n",
       "5            0             1         NaN               10  \n",
       "6            0             1         1.0               10  \n",
       "7            0             1         1.0               10  \n",
       "8            0             0         NaN               10  \n",
       "9            0             1         NaN               10  \n",
       "\n",
       "[10 rows x 28 columns]"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# visu de la table\n",
    "customerplus_clean_spectacle.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "738e063b-f84e-4a00-b35d-6d1d657e3c09",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nombre de lignes de la table :  1523684\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "customer_id                     0\n",
       "street_id                       0\n",
       "structure_id              1460622\n",
       "mcp_contact_id             729163\n",
       "fidelity                        0\n",
       "tenant_id                       0\n",
       "is_partner                      0\n",
       "deleted_at                1523684\n",
       "gender                          0\n",
       "is_email_true                   0\n",
       "opt_in                          0\n",
       "last_buying_date           762879\n",
       "max_price                  762879\n",
       "ticket_sum                      0\n",
       "average_price              667328\n",
       "average_purchase_delay     762915\n",
       "average_price_basket       762915\n",
       "average_ticket_basket      762915\n",
       "total_price                 95551\n",
       "purchase_count                  0\n",
       "first_buying_date          762879\n",
       "country                    429485\n",
       "gender_label                    0\n",
       "gender_female                   0\n",
       "gender_male                     0\n",
       "gender_other                    0\n",
       "country_fr                 429485\n",
       "number_compagny                 0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# nombre de NaN\n",
    "print(\"Nombre de lignes de la table : \",customerplus_clean_spectacle.shape[0])\n",
    "customerplus_clean_spectacle.isna().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b44054b3-d850-4bc9-bc73-feb9979908bc",
   "metadata": {},
   "source": [
    "#### Nombre de clients de la compagnie"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "884a33d0-c275-4ab4-ab1f-8b53e563fb95",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   number_compagny  already_purchased  customer_id\n",
      "0               10               True        45263\n",
      "1               11               True        35312\n",
      "2               12               True       216104\n",
      "3               13               True       388730\n",
      "4               14               True       101642\n",
      "   number_compagny  already_purchased  customer_id\n",
      "0               10              False        53530\n",
      "1               11              False        35994\n",
      "2               12              False        26620\n",
      "3               13              False       379005\n",
      "4               14              False       241484\n"
     ]
    }
   ],
   "source": [
    "# nouveau barplot pr les clients : on regarde la taille totale de la base et on distingue clients ayant acheté / pas acheté\n",
    "\n",
    "# variable relative à l'achat\n",
    "customerplus_clean_spectacle[\"already_purchased\"] = customerplus_clean_spectacle[\"purchase_count\"]>0\n",
    "\n",
    "nb_customers_purchasing_spectacle = customerplus_clean_spectacle[customerplus_clean_spectacle[\"already_purchased\"]].groupby([\"number_compagny\",\"already_purchased\"])[\"customer_id\"].count().reset_index()\n",
    "nb_customers_no_purchase_spectacle = customerplus_clean_spectacle[~customerplus_clean_spectacle[\"already_purchased\"]].groupby([\"number_compagny\",\"already_purchased\"])[\"customer_id\"].count().reset_index()\n",
    "\n",
    "print(nb_customers_purchasing_spectacle)\n",
    "print(nb_customers_no_purchase_spectacle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "41c9fb5a-708b-4f85-9918-00337151f155",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 37672 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Création du barplot\n",
    "plt.bar(nb_customers_purchasing_spectacle[\"number_compagny\"], nb_customers_purchasing_spectacle[\"customer_id\"]/1000, label = \"clients ayant acheté\")\n",
    "plt.bar(nb_customers_no_purchase_spectacle[\"number_compagny\"], nb_customers_no_purchase_spectacle[\"customer_id\"]/1000, \n",
    "        bottom = nb_customers_purchasing_spectacle[\"customer_id\"]/1000, label = \"clients ciblés par un mail\")\n",
    "\n",
    "\n",
    "# Ajout de titres et d'étiquettes\n",
    "plt.xlabel('Compagnie')\n",
    "plt.ylabel(\"Nombre de clients (en milliers)\")\n",
    "plt.title(\"Nombre de clients identifiés pour les compagnies de spectacle\")\n",
    "plt.legend()\n",
    "\n",
    "# Affichage du barplot\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "a41dfb3e-12b6-4a7b-9282-698d9476b17b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 37672 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# syntaxe à retenir pr exporter des images !!\n",
    "\n",
    "\n",
    "FILE_PATH = \"projet-bdc2324-team1/graphics/music/\"\n",
    "FILE_NAME = \"number_customers_music.png\"\n",
    "FILE_PATH_OUT_S3 = FILE_PATH + FILE_NAME\n",
    "\n",
    "# Création du barplot\n",
    "plt.bar(nb_customers_purchasing_spectacle[\"number_compagny\"], nb_customers_purchasing_spectacle[\"customer_id\"]/1000, label = \"clients ayant acheté\")\n",
    "plt.bar(nb_customers_no_purchase_spectacle[\"number_compagny\"], nb_customers_no_purchase_spectacle[\"customer_id\"]/1000, \n",
    "        bottom = nb_customers_purchasing_spectacle[\"customer_id\"]/1000, label = \"clients ciblés par un mail\")\n",
    "\n",
    "\n",
    "# Ajout de titres et d'étiquettes\n",
    "plt.xlabel('Compagnie')\n",
    "plt.ylabel(\"Nombre de clients (en milliers)\")\n",
    "plt.title(\"Nombre de clients identifiés pour les compagnies de spectacle\")\n",
    "plt.legend()\n",
    "\n",
    "with fs.open(FILE_PATH_OUT_S3, 'wb') as file_out:\n",
    "    plt.savefig(file_out)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85b6c7a9-d970-4071-8633-45bc1f50e157",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "#### Prix maximal payé par un client (utile ??)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "id": "fd11c547-7128-4ef6-ad7b-4b7c2a30cd9e",
   "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>number_compagny</th>\n",
       "      <th>max_price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>13823.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11</td>\n",
       "      <td>108.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12</td>\n",
       "      <td>5000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>13</td>\n",
       "      <td>3180.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14</td>\n",
       "      <td>456.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number_compagny  max_price\n",
       "0               10    13823.0\n",
       "1               11      108.0\n",
       "2               12     5000.0\n",
       "3               13     3180.0\n",
       "4               14      456.0"
      ]
     },
     "execution_count": 152,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# prix maximal payé par un client pour chaque compagnie - très variable : de 108 à 13823\n",
    "\n",
    "company_max_price = customerplus_clean_spectacle.groupby(\"number_compagny\")[\"max_price\"].max().reset_index()\n",
    "company_max_price"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "id": "b8f8f162-4153-4cfe-bfaa-d981d414510d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 31880 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Création du barplot\n",
    "plt.bar(company_max_price[\"number_compagny\"], 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 spectacle\")\n",
    "\n",
    "# Affichage du barplot\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "bff23e5d-d7ed-4092-ae3c-5df503e54a6d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    762879.000000\n",
       "mean          0.079068\n",
       "std           3.969729\n",
       "min           0.000000\n",
       "25%           0.000000\n",
       "50%           0.000000\n",
       "75%           0.000000\n",
       "max        3334.000000\n",
       "Name: purchase_count, dtype: float64"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customerplus_clean_spectacle[customerplus_clean_spectacle[\"first_buying_date\"].isna()][\"purchase_count\"].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "89466dbd-14d2-4ede-9ca0-b9c32b764e25",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    7.608090e+05\n",
       "mean     3.863940e+00\n",
       "std      1.685825e+03\n",
       "min      1.000000e+00\n",
       "25%      1.000000e+00\n",
       "50%      1.000000e+00\n",
       "75%      2.000000e+00\n",
       "max      1.469325e+06\n",
       "Name: purchase_count, dtype: float64"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customerplus_clean_spectacle[~customerplus_clean_spectacle[\"first_buying_date\"].isna()][\"purchase_count\"].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "5f9feae4-35f4-43b6-adeb-f75773900a2d",
   "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>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>has_tags</th>\n",
       "      <th>number_compagny</th>\n",
       "      <th>already_purchased</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>821538</td>\n",
       "      <td>139</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>809126</td>\n",
       "      <td>1063</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>fr</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>11005</td>\n",
       "      <td>1063</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>fr</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>17663</td>\n",
       "      <td>12731</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "      <td>...</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>0</td>\n",
       "      <td>10</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>38100</td>\n",
       "      <td>12395</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</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>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>0</td>\n",
       "      <td>10</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",
       "      <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>343121</th>\n",
       "      <td>4667645</td>\n",
       "      <td>122</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1534181.0</td>\n",
       "      <td>0</td>\n",
       "      <td>862</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>14</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>343122</th>\n",
       "      <td>4667649</td>\n",
       "      <td>122</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1534177.0</td>\n",
       "      <td>0</td>\n",
       "      <td>862</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>14</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>343123</th>\n",
       "      <td>4667660</td>\n",
       "      <td>122</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1534165.0</td>\n",
       "      <td>0</td>\n",
       "      <td>862</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>NaN</td>\n",
       "      <td>female</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>14</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>343124</th>\n",
       "      <td>4667679</td>\n",
       "      <td>122</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1534132.0</td>\n",
       "      <td>0</td>\n",
       "      <td>862</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>14</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>343125</th>\n",
       "      <td>4667686</td>\n",
       "      <td>122</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1567949.0</td>\n",
       "      <td>0</td>\n",
       "      <td>862</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>NaN</td>\n",
       "      <td>female</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>14</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1523688 rows × 30 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        customer_id  street_id  structure_id  mcp_contact_id  fidelity  \\\n",
       "0            821538        139           NaN             NaN         0   \n",
       "1            809126       1063           NaN             NaN         0   \n",
       "2             11005       1063           NaN             NaN         0   \n",
       "3             17663      12731           NaN             NaN         0   \n",
       "4             38100      12395           NaN             NaN         0   \n",
       "...             ...        ...           ...             ...       ...   \n",
       "343121      4667645        122           NaN       1534181.0         0   \n",
       "343122      4667649        122           NaN       1534177.0         0   \n",
       "343123      4667660        122           NaN       1534165.0         0   \n",
       "343124      4667679        122           NaN       1534132.0         0   \n",
       "343125      4667686        122           NaN       1567949.0         0   \n",
       "\n",
       "        tenant_id  is_partner  deleted_at  gender  is_email_true  ...  \\\n",
       "0             875       False         NaN       2           True  ...   \n",
       "1             875       False         NaN       2           True  ...   \n",
       "2             875       False         NaN       2          False  ...   \n",
       "3             875       False         NaN       0          False  ...   \n",
       "4             875       False         NaN       0           True  ...   \n",
       "...           ...         ...         ...     ...            ...  ...   \n",
       "343121        862       False         NaN       2           True  ...   \n",
       "343122        862       False         NaN       2           True  ...   \n",
       "343123        862       False         NaN       0           True  ...   \n",
       "343124        862       False         NaN       2           True  ...   \n",
       "343125        862       False         NaN       0           True  ...   \n",
       "\n",
       "        first_buying_date country  gender_label  gender_female  gender_male  \\\n",
       "0                     NaN     NaN         other              0            0   \n",
       "1                     NaN      fr         other              0            0   \n",
       "2                     NaN      fr         other              0            0   \n",
       "3                     NaN      fr        female              1            0   \n",
       "4                     NaN      fr        female              1            0   \n",
       "...                   ...     ...           ...            ...          ...   \n",
       "343121                NaN     NaN         other              0            0   \n",
       "343122                NaN     NaN         other              0            0   \n",
       "343123                NaN     NaN        female              1            0   \n",
       "343124                NaN     NaN         other              0            0   \n",
       "343125                NaN     NaN        female              1            0   \n",
       "\n",
       "        gender_other  country_fr  has_tags  number_compagny  already_purchased  \n",
       "0                  1         NaN         0               10              False  \n",
       "1                  1         1.0         0               10              False  \n",
       "2                  1         1.0         0               10              False  \n",
       "3                  0         1.0         0               10              False  \n",
       "4                  0         1.0         0               10              False  \n",
       "...              ...         ...       ...              ...                ...  \n",
       "343121             1         NaN         0               14              False  \n",
       "343122             1         NaN         0               14              False  \n",
       "343123             0         NaN         0               14              False  \n",
       "343124             1         NaN         0               14              False  \n",
       "343125             0         NaN         0               14              False  \n",
       "\n",
       "[1523688 rows x 30 columns]"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customerplus_clean_spectacle[\"already_purchased\"] = customerplus_clean_spectacle[\"first_buying_date\"].isna()==False\n",
    "customerplus_clean_spectacle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "cec4f1eb-cec8-409d-8b2c-1e01f1bf81ff",
   "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>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>has_tags</th>\n",
       "      <th>number_compagny</th>\n",
       "      <th>already_purchased</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>11005</td>\n",
       "      <td>1063</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>fr</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>17663</td>\n",
       "      <td>12731</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "      <td>...</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>0</td>\n",
       "      <td>10</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>38100</td>\n",
       "      <td>12395</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</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>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>0</td>\n",
       "      <td>10</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>307036</td>\n",
       "      <td>139</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2946</td>\n",
       "      <td>1063</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>875</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>False</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>fr</td>\n",
       "      <td>other</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>10</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",
       "      <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>338933</th>\n",
       "      <td>3625705</td>\n",
       "      <td>648752</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1253864.0</td>\n",
       "      <td>0</td>\n",
       "      <td>862</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>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>0</td>\n",
       "      <td>14</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>338954</th>\n",
       "      <td>3627626</td>\n",
       "      <td>636890</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1253887.0</td>\n",
       "      <td>0</td>\n",
       "      <td>862</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>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>0</td>\n",
       "      <td>14</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>338959</th>\n",
       "      <td>3628124</td>\n",
       "      <td>653042</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1253899.0</td>\n",
       "      <td>0</td>\n",
       "      <td>862</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>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>0</td>\n",
       "      <td>14</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>338986</th>\n",
       "      <td>3631189</td>\n",
       "      <td>648423</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1253928.0</td>\n",
       "      <td>0</td>\n",
       "      <td>862</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>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>0</td>\n",
       "      <td>14</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>339039</th>\n",
       "      <td>3635380</td>\n",
       "      <td>659417</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1253975.0</td>\n",
       "      <td>0</td>\n",
       "      <td>862</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>fr</td>\n",
       "      <td>male</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>14</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>26246 rows × 30 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        customer_id  street_id  structure_id  mcp_contact_id  fidelity  \\\n",
       "2             11005       1063           NaN             NaN         0   \n",
       "3             17663      12731           NaN             NaN         0   \n",
       "4             38100      12395           NaN             NaN         0   \n",
       "5            307036        139           NaN             NaN         0   \n",
       "6              2946       1063           NaN             NaN         0   \n",
       "...             ...        ...           ...             ...       ...   \n",
       "338933      3625705     648752           NaN       1253864.0         0   \n",
       "338954      3627626     636890           NaN       1253887.0         0   \n",
       "338959      3628124     653042           NaN       1253899.0         0   \n",
       "338986      3631189     648423           NaN       1253928.0         0   \n",
       "339039      3635380     659417           NaN       1253975.0         0   \n",
       "\n",
       "        tenant_id  is_partner  deleted_at  gender  is_email_true  ...  \\\n",
       "2             875       False         NaN       2          False  ...   \n",
       "3             875       False         NaN       0          False  ...   \n",
       "4             875       False         NaN       0           True  ...   \n",
       "5             875       False         NaN       2           True  ...   \n",
       "6             875       False         NaN       2          False  ...   \n",
       "...           ...         ...         ...     ...            ...  ...   \n",
       "338933        862       False         NaN       0           True  ...   \n",
       "338954        862       False         NaN       0           True  ...   \n",
       "338959        862       False         NaN       0           True  ...   \n",
       "338986        862       False         NaN       0           True  ...   \n",
       "339039        862       False         NaN       1           True  ...   \n",
       "\n",
       "        first_buying_date country  gender_label  gender_female  gender_male  \\\n",
       "2                     NaN      fr         other              0            0   \n",
       "3                     NaN      fr        female              1            0   \n",
       "4                     NaN      fr        female              1            0   \n",
       "5                     NaN     NaN         other              0            0   \n",
       "6                     NaN      fr         other              0            0   \n",
       "...                   ...     ...           ...            ...          ...   \n",
       "338933                NaN      fr        female              1            0   \n",
       "338954                NaN      fr        female              1            0   \n",
       "338959                NaN      fr        female              1            0   \n",
       "338986                NaN      fr        female              1            0   \n",
       "339039                NaN      fr          male              0            1   \n",
       "\n",
       "        gender_other  country_fr  has_tags  number_compagny  already_purchased  \n",
       "2                  1         1.0         0               10              False  \n",
       "3                  0         1.0         0               10              False  \n",
       "4                  0         1.0         0               10              False  \n",
       "5                  1         NaN         0               10              False  \n",
       "6                  1         1.0         0               10              False  \n",
       "...              ...         ...       ...              ...                ...  \n",
       "338933             0         1.0         0               14              False  \n",
       "338954             0         1.0         0               14              False  \n",
       "338959             0         1.0         0               14              False  \n",
       "338986             0         1.0         0               14              False  \n",
       "339039             0         1.0         0               14              False  \n",
       "\n",
       "[26246 rows x 30 columns]"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# attention, on a des cas où le client a pas de première date d'achat alors qu'il compte plusieurs achats\n",
    "# on peut donc avoir une date de première achat valant NaN non pas parce que l'individu n'a jamais acheté \n",
    "# mais simplement car elle n'est pas renseignée\n",
    "\n",
    "customerplus_clean_spectacle[(customerplus_clean_spectacle[\"already_purchased\"]==False) &\n",
    "(customerplus_clean_spectacle[\"purchase_count\"]>0)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "b5904039-a967-47d5-ba13-1b805bcd76ca",
   "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>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>has_tags</th>\n",
       "      <th>number_compagny</th>\n",
       "      <th>already_purchased</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: [customer_id, street_id, structure_id, mcp_contact_id, fidelity, tenant_id, is_partner, deleted_at, gender, is_email_true, opt_in, last_buying_date, max_price, ticket_sum, average_price, average_purchase_delay, average_price_basket, average_ticket_basket, total_price, purchase_count, first_buying_date, country, gender_label, gender_female, gender_male, gender_other, country_fr, has_tags, number_compagny, already_purchased]\n",
       "Index: []\n",
       "\n",
       "[0 rows x 30 columns]"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# cpdt, si un client a un nombre d'achats nul, il a bien une date de premier achat valant NaN, OK\n",
    "customerplus_clean_spectacle[(customerplus_clean_spectacle[\"already_purchased\"]) &\n",
    "(customerplus_clean_spectacle[\"purchase_count\"]==0)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "703d9986-4497-404f-881a-45ca44b25beb",
   "metadata": {},
   "source": [
    "#### différence de consentement aux campagnes de mails (opt in)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "e940bfcf-29cc-4d4c-ae5e-e2a8cecf28af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "number_compagny  already_purchased\n",
       "10               False                0.234840\n",
       "                 True                 0.236242\n",
       "11               False                0.141746\n",
       "                 True                 0.002804\n",
       "12               False                0.485950\n",
       "                 True                 0.244780\n",
       "13               False                0.084057\n",
       "                 True                 0.177213\n",
       "14               False                0.885553\n",
       "                 True                 0.308859\n",
       "Name: opt_in, dtype: float64"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# différence de consentement aux campagnes de mails (opt in)\n",
    "\n",
    "# en se restreignant au personnes n'ayant pas acheté, on a quand même des individus acceptant d'être ciblés\n",
    "customerplus_clean_spectacle[customerplus_clean_spectacle[\"first_buying_date\"].isna()][\"opt_in\"].unique()\n",
    "\n",
    "# taux de consentement variés\n",
    "customerplus_clean_spectacle[\"already_purchased\"] = customerplus_clean_spectacle[\"purchase_count\"] > 0\n",
    "customerplus_clean_spectacle.groupby([\"number_compagny\", \"already_purchased\"])[\"opt_in\"].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 209,
   "id": "a5e79beb-9ba0-4c89-b084-e27ff0d65dcc",
   "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>number_compagny</th>\n",
       "      <th>already_purchased</th>\n",
       "      <th>opt_in</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>False</td>\n",
       "      <td>0.234840</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10</td>\n",
       "      <td>True</td>\n",
       "      <td>0.236242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>11</td>\n",
       "      <td>False</td>\n",
       "      <td>0.141746</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>11</td>\n",
       "      <td>True</td>\n",
       "      <td>0.002804</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>12</td>\n",
       "      <td>False</td>\n",
       "      <td>0.485950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>12</td>\n",
       "      <td>True</td>\n",
       "      <td>0.244780</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>13</td>\n",
       "      <td>False</td>\n",
       "      <td>0.084057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>13</td>\n",
       "      <td>True</td>\n",
       "      <td>0.177213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>14</td>\n",
       "      <td>False</td>\n",
       "      <td>0.885553</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>14</td>\n",
       "      <td>True</td>\n",
       "      <td>0.308859</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number_compagny  already_purchased    opt_in\n",
       "0               10              False  0.234840\n",
       "1               10               True  0.236242\n",
       "2               11              False  0.141746\n",
       "3               11               True  0.002804\n",
       "4               12              False  0.485950\n",
       "5               12               True  0.244780\n",
       "6               13              False  0.084057\n",
       "7               13               True  0.177213\n",
       "8               14              False  0.885553\n",
       "9               14               True  0.308859"
      ]
     },
     "execution_count": 209,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_graph = customerplus_clean_spectacle.groupby([\"number_compagny\", \"already_purchased\"])[\"opt_in\"].mean().reset_index()\n",
    "df_graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 210,
   "id": "5be56c41-7697-481a-84ea-f77a2041484b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 34204 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Création du barplot groupé\n",
    "fig, ax = plt.subplots(figsize=(10, 6))\n",
    "\n",
    "categories = df_graph[\"number_compagny\"].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_compagny'] == category]['opt_in'].values[0]*100 for category in categories]\n",
    "\n",
    "    label_printed = \"client ayant déjà acheté\" if label else \"client n'ayant jamais acheté\"\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('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",
    "# sauvegarde dans le MinIO\n",
    "\n",
    "FILE_NAME = \"consent_customers_music.png\"\n",
    "FILE_PATH_OUT_S3 = FILE_PATH + FILE_NAME\n",
    "\n",
    "with fs.open(FILE_PATH_OUT_S3, 'wb') as file_out:\n",
    "    plt.savefig(file_out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 211,
   "id": "91b743c4-5473-41e1-b97e-cf06904f0fa8",
   "metadata": {
    "scrolled": true
   },
   "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>number_company</th>\n",
       "      <th>y_has_purchased</th>\n",
       "      <th>opt_in</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>0.0</td>\n",
       "      <td>55.896356</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10</td>\n",
       "      <td>1.0</td>\n",
       "      <td>50.795672</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>11</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.856590</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>11</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.046125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>37.098498</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>12</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.021608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>13</td>\n",
       "      <td>0.0</td>\n",
       "      <td>32.457022</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>13</td>\n",
       "      <td>1.0</td>\n",
       "      <td>19.461217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>14</td>\n",
       "      <td>0.0</td>\n",
       "      <td>69.470107</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>14</td>\n",
       "      <td>1.0</td>\n",
       "      <td>26.682793</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number_company  y_has_purchased     opt_in\n",
       "0              10              0.0  55.896356\n",
       "1              10              1.0  50.795672\n",
       "2              11              0.0   4.856590\n",
       "3              11              1.0   0.046125\n",
       "4              12              0.0  37.098498\n",
       "5              12              1.0   0.021608\n",
       "6              13              0.0  32.457022\n",
       "7              13              1.0  19.461217\n",
       "8              14              0.0  69.470107\n",
       "9              14              1.0  26.682793"
      ]
     },
     "execution_count": 211,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# on refait le graphique sur train set \n",
    "\n",
    "df_graph = train_set_spectacle.groupby([\"number_company\", \"y_has_purchased\"])[\"opt_in\"].mean().reset_index()\n",
    "df_graph[\"opt_in\"] = 100 * df_graph[\"opt_in\"]\n",
    "df_graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "id": "728e0021-4f95-4601-bb01-032db2cf6571",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.43006504592722195\n",
      "0.2889608343987336\n"
     ]
    }
   ],
   "source": [
    "# pourquoi une telle différence sur la variable opt in ??\n",
    "print(train_set_spectacle[\"opt_in\"].mean())\n",
    "print(customerplus_clean_spectacle[\"opt_in\"].mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "274b4bc5-277f-476a-8bc1-c1764b1df2de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.8473746548562269\n",
      "0.7573747808905485\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "id": "e1d837e1-c445-424b-867a-48b1e790f703",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "genre = homme : \n",
      "0.3754292890099192\n",
      "0.3103924435775397\n",
      "email vérifié : \n",
      "0.9966249488521722\n",
      "0.936015604285403\n",
      "nationalité française : \n",
      "0.7882316165225254\n",
      "0.7573741156773128\n",
      "nbre d'achats : \n",
      "1.7069010765735895\n",
      "0.9938799646120849\n"
     ]
    }
   ],
   "source": [
    "# pour les autres variables, la distribution semble similaire\n",
    "\n",
    "print(\"genre = homme : \")\n",
    "print(train_set_spectacle[\"gender_male\"].mean())\n",
    "print(customerplus_clean_spectacle[\"gender_male\"].mean())\n",
    "\n",
    "print(\"email vérifié : \")\n",
    "print(train_set_spectacle[\"is_email_true\"].mean())\n",
    "print(customerplus_clean_spectacle[\"is_email_true\"].mean())\n",
    "\n",
    "print(\"nationalité française : \")\n",
    "print(train_set_spectacle[\"country_fr\"].mean())\n",
    "print(customerplus_clean_spectacle[\"country_fr\"].mean())\n",
    "\n",
    "# sauf pr nbre d'achats - à verif\n",
    "print(\"nbre d'achats : \")\n",
    "print(train_set_spectacle[\"purchase_count\"].mean())\n",
    "print(customerplus_clean_spectacle[\"purchase_count\"].mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 214,
   "id": "43deeeb5-8092-42fc-b80b-59d2c58093de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 36364 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# with the generic function\n",
    "multiple_barplot(df_graph, x=\"number_company\", y=\"opt_in\", var_labels=\"y_has_purchased\",\n",
    "                dico_labels = {0 : \"aucun achat\", 1 : \"achat durant la période\"},\n",
    "                xlabel = \"Numéro de compagnie\", ylabel = \"Part de consentement (%)\", \n",
    "                title = \"Part de consentement au mailing selon les compagnies (train set)\")\n",
    "\n",
    "# save in the s3\n",
    "\n",
    "FILE_NAME = \"consent_customers_train_set_music.png\"\n",
    "FILE_PATH_OUT_S3 = FILE_PATH + FILE_NAME\n",
    "\n",
    "with fs.open(FILE_PATH_OUT_S3, 'wb') as file_out:\n",
    "    plt.savefig(file_out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 213,
   "id": "360047fc-70a4-4876-b0f1-c0af5cc93e17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "5fcff5cb-923b-44d7-b345-0bee89d30ea2",
   "metadata": {},
   "source": [
    "#### Etude du genre"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 216,
   "id": "32960530-cb46-4eeb-a6d2-1dcf5fb640d8",
   "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>number_compagny</th>\n",
       "      <th>gender_male</th>\n",
       "      <th>gender_female</th>\n",
       "      <th>gender_other</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>0.181582</td>\n",
       "      <td>0.343840</td>\n",
       "      <td>0.474578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11</td>\n",
       "      <td>0.179522</td>\n",
       "      <td>0.314448</td>\n",
       "      <td>0.506030</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12</td>\n",
       "      <td>0.346381</td>\n",
       "      <td>0.454038</td>\n",
       "      <td>0.199581</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>13</td>\n",
       "      <td>0.318108</td>\n",
       "      <td>0.503093</td>\n",
       "      <td>0.178799</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14</td>\n",
       "      <td>0.331954</td>\n",
       "      <td>0.316181</td>\n",
       "      <td>0.351865</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number_compagny  gender_male  gender_female  gender_other\n",
       "0               10     0.181582       0.343840      0.474578\n",
       "1               11     0.179522       0.314448      0.506030\n",
       "2               12     0.346381       0.454038      0.199581\n",
       "3               13     0.318108       0.503093      0.178799\n",
       "4               14     0.331954       0.316181      0.351865"
      ]
     },
     "execution_count": 216,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# genre \n",
    "\n",
    "company_genders = customerplus_clean_spectacle.groupby(\"number_compagny\")[[\"gender_male\", \"gender_female\", \"gender_other\"]].mean().reset_index()\n",
    "company_genders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 217,
   "id": "1b4a49d7-7bfe-4e80-aa7e-c9c6d4bc46e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30288 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Création du barplot\n",
    "plt.bar(company_genders[\"number_compagny\"], company_genders[\"gender_male\"], label = \"Homme\")\n",
    "plt.bar(company_genders[\"number_compagny\"], company_genders[\"gender_female\"], \n",
    "        bottom = company_genders[\"gender_male\"], label = \"Femme\")\n",
    "\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 spectacle\")\n",
    "plt.legend()\n",
    "\n",
    "# Affichage du barplot\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 218,
   "id": "c7348c95-e506-4002-90d9-d3b6768af985",
   "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>number_company</th>\n",
       "      <th>y_has_purchased</th>\n",
       "      <th>gender_male</th>\n",
       "      <th>gender_female</th>\n",
       "      <th>gender_other</th>\n",
       "      <th>share_of_women</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.140862</td>\n",
       "      <td>0.288775</td>\n",
       "      <td>0.570363</td>\n",
       "      <td>67.213639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.284532</td>\n",
       "      <td>0.714831</td>\n",
       "      <td>0.000637</td>\n",
       "      <td>71.528662</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>11</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.289900</td>\n",
       "      <td>0.512669</td>\n",
       "      <td>0.197431</td>\n",
       "      <td>63.878535</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>11</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.321033</td>\n",
       "      <td>0.609779</td>\n",
       "      <td>0.069188</td>\n",
       "      <td>65.510406</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.357546</td>\n",
       "      <td>0.470654</td>\n",
       "      <td>0.171799</td>\n",
       "      <td>56.828519</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>12</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.396824</td>\n",
       "      <td>0.494058</td>\n",
       "      <td>0.109118</td>\n",
       "      <td>55.457191</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>13</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.363198</td>\n",
       "      <td>0.492956</td>\n",
       "      <td>0.143846</td>\n",
       "      <td>57.577983</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>13</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.379703</td>\n",
       "      <td>0.516605</td>\n",
       "      <td>0.103693</td>\n",
       "      <td>57.637000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>14</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.447676</td>\n",
       "      <td>0.443646</td>\n",
       "      <td>0.108678</td>\n",
       "      <td>49.773906</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>14</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.487695</td>\n",
       "      <td>0.471498</td>\n",
       "      <td>0.040808</td>\n",
       "      <td>49.155702</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number_company  y_has_purchased  gender_male  gender_female  gender_other  \\\n",
       "0              10              0.0     0.140862       0.288775      0.570363   \n",
       "1              10              1.0     0.284532       0.714831      0.000637   \n",
       "2              11              0.0     0.289900       0.512669      0.197431   \n",
       "3              11              1.0     0.321033       0.609779      0.069188   \n",
       "4              12              0.0     0.357546       0.470654      0.171799   \n",
       "5              12              1.0     0.396824       0.494058      0.109118   \n",
       "6              13              0.0     0.363198       0.492956      0.143846   \n",
       "7              13              1.0     0.379703       0.516605      0.103693   \n",
       "8              14              0.0     0.447676       0.443646      0.108678   \n",
       "9              14              1.0     0.487695       0.471498      0.040808   \n",
       "\n",
       "   share_of_women  \n",
       "0       67.213639  \n",
       "1       71.528662  \n",
       "2       63.878535  \n",
       "3       65.510406  \n",
       "4       56.828519  \n",
       "5       55.457191  \n",
       "6       57.577983  \n",
       "7       57.637000  \n",
       "8       49.773906  \n",
       "9       49.155702  "
      ]
     },
     "execution_count": 218,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sur le train set \n",
    "company_genders = train_set_spectacle.groupby([\"number_company\", \"y_has_purchased\"])[[\"gender_male\", \"gender_female\", \"gender_other\"]].mean().reset_index()\n",
    "company_genders[\"share_of_women\"] = 100 * (company_genders[\"gender_female\"]/(1-company_genders[\"gender_other\"]))\n",
    "company_genders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 219,
   "id": "b36e5a8f-45dc-4b74-8137-80b7e916aa84",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 34368 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# création barplot avec la fonction générique\n",
    "\n",
    "multiple_barplot(company_genders, x=\"number_company\", y=\"share_of_women\", var_labels=\"y_has_purchased\",\n",
    "                dico_labels = {0 : \"aucun achat\", 1 : \"achat durant la période\"},\n",
    "                xlabel = \"Numéro de compagnie\", ylabel = \"Part de femmes (%)\", \n",
    "                title = \"Part de femmes selon les compagnies de spectacle (train set)\")\n",
    "\n",
    "# save in the s3\n",
    "\n",
    "FILE_NAME = \"gender_train_set_music.png\"\n",
    "FILE_PATH_OUT_S3 = FILE_PATH + FILE_NAME\n",
    "\n",
    "with fs.open(FILE_PATH_OUT_S3, 'wb') as file_out:\n",
    "    plt.savefig(file_out)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9504e6b6-d97c-4aa9-a56a-f9f97264be05",
   "metadata": {},
   "source": [
    "#### Etude du pays d'origine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 220,
   "id": "ed6374e5-f36c-4f8e-9dba-602715b726f1",
   "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>number_compagny</th>\n",
       "      <th>country_fr</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>0.996136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11</td>\n",
       "      <td>0.994838</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12</td>\n",
       "      <td>0.002119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>13</td>\n",
       "      <td>0.831794</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14</td>\n",
       "      <td>0.993978</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number_compagny  country_fr\n",
       "0               10    0.996136\n",
       "1               11    0.994838\n",
       "2               12    0.002119\n",
       "3               13    0.831794\n",
       "4               14    0.993978"
      ]
     },
     "execution_count": 220,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pays d'origine (France VS reste du monde)\n",
    "\n",
    "company_country_fr = customerplus_clean_spectacle.groupby(\"number_compagny\")[\"country_fr\"].mean().reset_index()\n",
    "company_country_fr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "id": "8d95cdd9-2ab3-4c9a-8442-bb9b98e0dd18",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 24844 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Création du barplot\n",
    "plt.bar(company_country_fr[\"number_compagny\"], 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 spectacle\")\n",
    "\n",
    "# Affichage du barplot\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 222,
   "id": "b459f81f-6d30-44fa-ad65-e85acbf12fd2",
   "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>number_company</th>\n",
       "      <th>y_has_purchased</th>\n",
       "      <th>country_fr</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>0.0</td>\n",
       "      <td>99.833259</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10</td>\n",
       "      <td>1.0</td>\n",
       "      <td>99.935317</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>11</td>\n",
       "      <td>0.0</td>\n",
       "      <td>99.486493</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>11</td>\n",
       "      <td>1.0</td>\n",
       "      <td>99.808521</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.155933</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>12</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.079799</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>13</td>\n",
       "      <td>0.0</td>\n",
       "      <td>82.894264</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>13</td>\n",
       "      <td>1.0</td>\n",
       "      <td>94.744832</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>14</td>\n",
       "      <td>0.0</td>\n",
       "      <td>99.238475</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>14</td>\n",
       "      <td>1.0</td>\n",
       "      <td>99.032154</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number_company  y_has_purchased  country_fr\n",
       "0              10              0.0   99.833259\n",
       "1              10              1.0   99.935317\n",
       "2              11              0.0   99.486493\n",
       "3              11              1.0   99.808521\n",
       "4              12              0.0    0.155933\n",
       "5              12              1.0    0.079799\n",
       "6              13              0.0   82.894264\n",
       "7              13              1.0   94.744832\n",
       "8              14              0.0   99.238475\n",
       "9              14              1.0   99.032154"
      ]
     },
     "execution_count": 222,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# graphique sur le train set\n",
    "\n",
    "company_country_fr = train_set_spectacle.groupby([\"number_company\", \"y_has_purchased\"])[[\"country_fr\"]].mean().reset_index()\n",
    "company_country_fr[\"country_fr\"] = 100 * company_country_fr[\"country_fr\"]\n",
    "company_country_fr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 223,
   "id": "4a037b48-1d65-4ed3-a012-7d6f5a312533",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 36296 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# generic function to generate the barplot ON THE TRAIN SET - nationality\n",
    "\n",
    "multiple_barplot(company_country_fr, x=\"number_company\", y=\"country_fr\", var_labels=\"y_has_purchased\",\n",
    "                dico_labels = {0 : \"aucun achat\", 1 : \"achat durant la période\"},\n",
    "                xlabel = \"Numéro de compagnie\", ylabel = \"Part de clients français (%)\", \n",
    "                title = \"Part de clients français des compagnies de spectacle (train set)\")\n",
    "\n",
    "# save in the s3\n",
    "\n",
    "FILE_NAME = \"nationality_fr_train_set_music.png\"\n",
    "FILE_PATH_OUT_S3 = FILE_PATH + FILE_NAME\n",
    "\n",
    "with fs.open(FILE_PATH_OUT_S3, 'wb') as file_out:\n",
    "    plt.savefig(file_out)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ecfd112e-270a-4223-b80f-7e95e57d199d",
   "metadata": {},
   "source": [
    "### 2. campaigns_information"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "id": "b37e7ddf-321a-4ebe-9742-9e760a541d29",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nombre de lignes de la table :  688953\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "customer_id                 0\n",
       "nb_campaigns                0\n",
       "nb_campaigns_opened         0\n",
       "time_to_open           301495\n",
       "number_compagny             0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 189,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# nombre de nan\n",
    "print(\"Nombre de lignes de la table : \",campaigns_information_spectacle.shape[0])\n",
    "campaigns_information_spectacle.isna().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47c15a1d-bef8-4105-87f3-607958667569",
   "metadata": {},
   "source": [
    "#### Part de clients n'ouvrant jamais les mails"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 224,
   "id": "de1ecaac-25bb-4853-b8ab-3ef2ca6917ed",
   "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_compagny</th>\n",
       "      <th>no_campaign_opened</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>29</td>\n",
       "      <td>4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaT</td>\n",
       "      <td>10</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>37</td>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaT</td>\n",
       "      <td>10</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>39</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0 days 05:16:38</td>\n",
       "      <td>10</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>41</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0 days 01:12:29</td>\n",
       "      <td>10</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>44</td>\n",
       "      <td>4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaT</td>\n",
       "      <td>10</td>\n",
       "      <td>True</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>254699</th>\n",
       "      <td>6837769</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0 days 23:42:15</td>\n",
       "      <td>14</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>254700</th>\n",
       "      <td>6875038</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaT</td>\n",
       "      <td>14</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>254701</th>\n",
       "      <td>6875066</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaT</td>\n",
       "      <td>14</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>254702</th>\n",
       "      <td>6875099</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaT</td>\n",
       "      <td>14</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>254703</th>\n",
       "      <td>6875143</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0 days 01:17:01</td>\n",
       "      <td>14</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>688953 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        customer_id  nb_campaigns  nb_campaigns_opened    time_to_open  \\\n",
       "0                29             4                  0.0             NaT   \n",
       "1                37             3                  0.0             NaT   \n",
       "2                39             4                  1.0 0 days 05:16:38   \n",
       "3                41             4                  1.0 0 days 01:12:29   \n",
       "4                44             4                  0.0             NaT   \n",
       "...             ...           ...                  ...             ...   \n",
       "254699      6837769             1                  1.0 0 days 23:42:15   \n",
       "254700      6875038             1                  0.0             NaT   \n",
       "254701      6875066             1                  0.0             NaT   \n",
       "254702      6875099             1                  0.0             NaT   \n",
       "254703      6875143             1                  1.0 0 days 01:17:01   \n",
       "\n",
       "        number_compagny  no_campaign_opened  \n",
       "0                    10                True  \n",
       "1                    10                True  \n",
       "2                    10               False  \n",
       "3                    10               False  \n",
       "4                    10                True  \n",
       "...                 ...                 ...  \n",
       "254699               14               False  \n",
       "254700               14                True  \n",
       "254701               14                True  \n",
       "254702               14                True  \n",
       "254703               14               False  \n",
       "\n",
       "[688953 rows x 6 columns]"
      ]
     },
     "execution_count": 224,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# part de clients n'ouvrant jamais les mails par compagnie\n",
    "\n",
    "campaigns_information_spectacle[\"no_campaign_opened\"] = pd.isna(campaigns_information_spectacle[\"time_to_open\"])\n",
    "campaigns_information_spectacle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 225,
   "id": "b5a0060f-a9dd-435b-844f-b24674b8bc27",
   "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>number_compagny</th>\n",
       "      <th>no_campaign_opened</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>0.605656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11</td>\n",
       "      <td>0.294001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12</td>\n",
       "      <td>0.475719</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>13</td>\n",
       "      <td>0.353820</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14</td>\n",
       "      <td>0.428148</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number_compagny  no_campaign_opened\n",
       "0               10            0.605656\n",
       "1               11            0.294001\n",
       "2               12            0.475719\n",
       "3               13            0.353820\n",
       "4               14            0.428148"
      ]
     },
     "execution_count": 225,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "company_lazy_customers = campaigns_information_spectacle.groupby(\"number_compagny\")[\"no_campaign_opened\"].mean().reset_index()\n",
    "company_lazy_customers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 226,
   "id": "788c90e0-f13a-4804-ace7-e5159fddd7fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 31696 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Création du barplot\n",
    "plt.bar(company_lazy_customers[\"number_compagny\"], 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 spectacle\")\n",
    "\n",
    "# Affichage du barplot\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33233fb9-707d-44c0-80e2-a131756110a1",
   "metadata": {},
   "source": [
    "#### Taux d'ouverture des campagnes de mails"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 227,
   "id": "c48015c2-6451-4089-93b7-6d55d3b2e553",
   "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>number_compagny</th>\n",
       "      <th>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "      <th>ratio_campaigns_opened</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>734772</td>\n",
       "      <td>126151.0</td>\n",
       "      <td>0.171687</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11</td>\n",
       "      <td>342396</td>\n",
       "      <td>129833.0</td>\n",
       "      <td>0.379190</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12</td>\n",
       "      <td>3168123</td>\n",
       "      <td>810722.0</td>\n",
       "      <td>0.255900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>13</td>\n",
       "      <td>3218569</td>\n",
       "      <td>793581.0</td>\n",
       "      <td>0.246563</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14</td>\n",
       "      <td>2427043</td>\n",
       "      <td>723846.0</td>\n",
       "      <td>0.298242</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number_compagny  nb_campaigns  nb_campaigns_opened  ratio_campaigns_opened\n",
       "0               10        734772             126151.0                0.171687\n",
       "1               11        342396             129833.0                0.379190\n",
       "2               12       3168123             810722.0                0.255900\n",
       "3               13       3218569             793581.0                0.246563\n",
       "4               14       2427043             723846.0                0.298242"
      ]
     },
     "execution_count": 227,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# taux d'ouverture des campaigns\n",
    "\n",
    "company_campaigns_stats = campaigns_information_spectacle.groupby(\"number_compagny\")[[\"nb_campaigns\", \"nb_campaigns_opened\"]].sum().reset_index()\n",
    "company_campaigns_stats[\"ratio_campaigns_opened\"] = company_campaigns_stats[\"nb_campaigns_opened\"]  / company_campaigns_stats[\"nb_campaigns\"]\n",
    "company_campaigns_stats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 228,
   "id": "d06ab865-4832-4fe9-918b-e5ff72bebee4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27108 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Création du barplot\n",
    "plt.bar(company_campaigns_stats[\"number_compagny\"], 100 * company_campaigns_stats[\"ratio_campaigns_opened\"])\n",
    "\n",
    "# Ajout de titres et d'étiquettes\n",
    "plt.xlabel('Company')\n",
    "plt.ylabel(\"Taux d'ouverture (%)\")\n",
    "plt.title(\"Taux d'ouverture des campagnes de mails pour les compagnies de spectacle\")\n",
    "\n",
    "# Affichage du barplot\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 230,
   "id": "5c37e063-a717-4a8c-828e-b386b87e8409",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 31384 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# création d'un barplot permettant de visualiser les 2 indicateurs sur le même graphique\n",
    "\n",
    "# Création du premier barplot\n",
    "plt.bar(company_campaigns_stats[\"number_compagny\"], 100 * company_campaigns_stats[\"ratio_campaigns_opened\"],\n",
    "       label = \"taux d'ouverture\", alpha = 0.7)\n",
    "\n",
    "# Création du deuxième barplot à côté du premier\n",
    "bar_width = 0.4  # Largeur des barres\n",
    "indices2 = company_campaigns_stats[\"number_compagny\"] + bar_width\n",
    "plt.bar(indices2, 100 * (1 - company_lazy_customers[\"no_campaign_opened\"]), \n",
    "        label='Part de clients ouvrant des mails', alpha=0.7, width=bar_width)\n",
    "\n",
    "# Ajout des étiquettes et de la légende\n",
    "plt.xlabel('Compagnie')\n",
    "plt.ylabel('Taux (%)')\n",
    "plt.title('Lien entre taux d ouverture des mails et nombre de clients actifs')\n",
    "plt.legend()\n",
    "\n",
    "# save in the s3\n",
    "\n",
    "FILE_NAME = \"stats_mail_opening_music.png\"\n",
    "FILE_PATH_OUT_S3 = FILE_PATH + FILE_NAME\n",
    "\n",
    "with fs.open(FILE_PATH_OUT_S3, 'wb') as file_out:\n",
    "    plt.savefig(file_out)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "638ab84b-15a5-4e70-b140-f121c68c82f5",
   "metadata": {},
   "source": [
    "#### on refait les mêmes stats sur le train set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 231,
   "id": "4fdf4134-d32c-42c3-ab4f-36ad4783332c",
   "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_tickets</th>\n",
       "      <th>nb_purchases</th>\n",
       "      <th>total_amount</th>\n",
       "      <th>nb_suppliers</th>\n",
       "      <th>vente_internet_max</th>\n",
       "      <th>purchase_date_min</th>\n",
       "      <th>purchase_date_max</th>\n",
       "      <th>time_between_purchase</th>\n",
       "      <th>nb_tickets_internet</th>\n",
       "      <th>...</th>\n",
       "      <th>gender_female</th>\n",
       "      <th>gender_male</th>\n",
       "      <th>gender_other</th>\n",
       "      <th>country_fr</th>\n",
       "      <th>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "      <th>time_to_open</th>\n",
       "      <th>y_has_purchased</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>10_492779</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>550.0</td>\n",
       "      <td>550.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>8 days 04:08:27</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10_563424</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>550.0</td>\n",
       "      <td>550.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>0 days 01:39:58.555555555</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10_44369</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>550.0</td>\n",
       "      <td>550.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10_620271</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>550.0</td>\n",
       "      <td>550.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10_687644</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>550.0</td>\n",
       "      <td>550.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 42 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  customer_id  nb_tickets  nb_purchases  total_amount  nb_suppliers  \\\n",
       "0   10_492779         0.0           0.0           0.0           0.0   \n",
       "1   10_563424         0.0           0.0           0.0           0.0   \n",
       "2    10_44369         0.0           0.0           0.0           0.0   \n",
       "3   10_620271         0.0           0.0           0.0           0.0   \n",
       "4   10_687644         0.0           0.0           0.0           0.0   \n",
       "\n",
       "   vente_internet_max  purchase_date_min  purchase_date_max  \\\n",
       "0                 0.0              550.0              550.0   \n",
       "1                 0.0              550.0              550.0   \n",
       "2                 0.0              550.0              550.0   \n",
       "3                 0.0              550.0              550.0   \n",
       "4                 0.0              550.0              550.0   \n",
       "\n",
       "   time_between_purchase  nb_tickets_internet  ...  gender_female  \\\n",
       "0                   -1.0                  0.0  ...              1   \n",
       "1                   -1.0                  0.0  ...              0   \n",
       "2                   -1.0                  0.0  ...              0   \n",
       "3                   -1.0                  0.0  ...              0   \n",
       "4                   -1.0                  0.0  ...              0   \n",
       "\n",
       "   gender_male  gender_other  country_fr  nb_campaigns  nb_campaigns_opened  \\\n",
       "0            0             0         1.0          13.0                  4.0   \n",
       "1            0             1         1.0          10.0                  9.0   \n",
       "2            1             0         1.0          14.0                  0.0   \n",
       "3            0             1         NaN           9.0                  0.0   \n",
       "4            0             1         NaN           4.0                  0.0   \n",
       "\n",
       "                time_to_open  y_has_purchased  number_company  \\\n",
       "0            8 days 04:08:27              0.0              10   \n",
       "1  0 days 01:39:58.555555555              0.0              10   \n",
       "2                        NaN              0.0              10   \n",
       "3                        NaN              0.0              10   \n",
       "4                        NaN              0.0              10   \n",
       "\n",
       "   no_campaign_opened  \n",
       "0               False  \n",
       "1               False  \n",
       "2                True  \n",
       "3                True  \n",
       "4                True  \n",
       "\n",
       "[5 rows x 42 columns]"
      ]
     },
     "execution_count": 231,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# same statistics on the train set\n",
    "\n",
    "train_set_spectacle.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "924300e5-d6a9-4686-a938-f5f99afda70c",
   "metadata": {},
   "source": [
    "#### Part de clients n'ouvrant aucun mail"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 232,
   "id": "14ff9886-742c-4a60-8824-5d31f7c76aea",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_set_spectacle[\"no_campaign_opened\"] = train_set_spectacle[\"nb_campaigns_opened\"]==0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 235,
   "id": "16285593-a0fa-461c-aeb8-c64ffdf9a0d6",
   "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>number_company</th>\n",
       "      <th>y_has_purchased</th>\n",
       "      <th>no_campaign_opened</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>0.0</td>\n",
       "      <td>73.553379</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10</td>\n",
       "      <td>1.0</td>\n",
       "      <td>35.582432</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>11</td>\n",
       "      <td>0.0</td>\n",
       "      <td>42.609537</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>11</td>\n",
       "      <td>1.0</td>\n",
       "      <td>32.887454</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>100.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>12</td>\n",
       "      <td>1.0</td>\n",
       "      <td>100.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>13</td>\n",
       "      <td>0.0</td>\n",
       "      <td>68.335897</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>13</td>\n",
       "      <td>1.0</td>\n",
       "      <td>52.833256</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>14</td>\n",
       "      <td>0.0</td>\n",
       "      <td>44.334881</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>14</td>\n",
       "      <td>1.0</td>\n",
       "      <td>28.807320</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number_company  y_has_purchased  no_campaign_opened\n",
       "0              10              0.0           73.553379\n",
       "1              10              1.0           35.582432\n",
       "2              11              0.0           42.609537\n",
       "3              11              1.0           32.887454\n",
       "4              12              0.0          100.000000\n",
       "5              12              1.0          100.000000\n",
       "6              13              0.0           68.335897\n",
       "7              13              1.0           52.833256\n",
       "8              14              0.0           44.334881\n",
       "9              14              1.0           28.807320"
      ]
     },
     "execution_count": 235,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "company_lazy_customers = train_set_spectacle.groupby([\"number_company\", \"y_has_purchased\"])[\"no_campaign_opened\"].mean().reset_index()\n",
    "company_lazy_customers[\"no_campaign_opened\"] = 100 * company_lazy_customers[\"no_campaign_opened\"] \n",
    "company_lazy_customers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 236,
   "id": "d35f00e3-b9b0-42b3-9dce-785c1ad5506c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 38548 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# graphic for non opening mails customers for music companies (train set)\n",
    "\n",
    "multiple_barplot(company_lazy_customers, x=\"number_company\", y=\"no_campaign_opened\", var_labels=\"y_has_purchased\",\n",
    "                dico_labels = {0 : \"aucun achat\", 1 : \"achat durant la période\"},\n",
    "                xlabel = \"Compagnie\", ylabel = \"Part de clients n'ayant ouvert aucun mail (%)\", \n",
    "                title = \"Part de clients des compagnies de spectacle n'ouvrant aucun mail (train set)\")\n",
    "\n",
    "# save in the s3\n",
    "\n",
    "FILE_NAME = \"no_mail_opened_train_set_music.png\"\n",
    "FILE_PATH_OUT_S3 = FILE_PATH + FILE_NAME\n",
    "\n",
    "with fs.open(FILE_PATH_OUT_S3, 'wb') as file_out:\n",
    "    plt.savefig(file_out)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3407307-7cc1-4f57-a3ae-7c83773b4b81",
   "metadata": {},
   "source": [
    "#### Part globale de mails ouverts pour chaque compagnie"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 237,
   "id": "b391f5b2-2424-4758-8ae5-f0fdacdfae66",
   "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_tickets</th>\n",
       "      <th>nb_purchases</th>\n",
       "      <th>total_amount</th>\n",
       "      <th>nb_suppliers</th>\n",
       "      <th>vente_internet_max</th>\n",
       "      <th>purchase_date_min</th>\n",
       "      <th>purchase_date_max</th>\n",
       "      <th>time_between_purchase</th>\n",
       "      <th>nb_tickets_internet</th>\n",
       "      <th>...</th>\n",
       "      <th>gender_female</th>\n",
       "      <th>gender_male</th>\n",
       "      <th>gender_other</th>\n",
       "      <th>country_fr</th>\n",
       "      <th>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "      <th>time_to_open</th>\n",
       "      <th>y_has_purchased</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>10_492779</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>550.000000</td>\n",
       "      <td>550.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>8 days 04:08:27</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10_563424</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>550.000000</td>\n",
       "      <td>550.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>0 days 01:39:58.555555555</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10_44369</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>550.000000</td>\n",
       "      <td>550.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10_620271</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>550.000000</td>\n",
       "      <td>550.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10_687644</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>550.000000</td>\n",
       "      <td>550.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10</td>\n",
       "      <td>True</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>354360</th>\n",
       "      <td>14_4685578</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>550.000000</td>\n",
       "      <td>550.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>14</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>354361</th>\n",
       "      <td>14_4652175</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>550.000000</td>\n",
       "      <td>550.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3 days 06:21:17</td>\n",
       "      <td>0.0</td>\n",
       "      <td>14</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>354362</th>\n",
       "      <td>14_4736169</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>91.030556</td>\n",
       "      <td>91.020139</td>\n",
       "      <td>0.010417</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0 days 17:30:10.166666666</td>\n",
       "      <td>1.0</td>\n",
       "      <td>14</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>354363</th>\n",
       "      <td>14_4957203</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>52.284028</td>\n",
       "      <td>52.284028</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>14</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>354364</th>\n",
       "      <td>14_4690653</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>550.000000</td>\n",
       "      <td>550.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>14</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>354365 rows × 42 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       customer_id  nb_tickets  nb_purchases  total_amount  nb_suppliers  \\\n",
       "0        10_492779         0.0           0.0           0.0           0.0   \n",
       "1        10_563424         0.0           0.0           0.0           0.0   \n",
       "2         10_44369         0.0           0.0           0.0           0.0   \n",
       "3        10_620271         0.0           0.0           0.0           0.0   \n",
       "4        10_687644         0.0           0.0           0.0           0.0   \n",
       "...            ...         ...           ...           ...           ...   \n",
       "354360  14_4685578         0.0           0.0           0.0           0.0   \n",
       "354361  14_4652175         0.0           0.0           0.0           0.0   \n",
       "354362  14_4736169         2.0           2.0          50.0           1.0   \n",
       "354363  14_4957203         1.0           1.0          55.0           1.0   \n",
       "354364  14_4690653         0.0           0.0           0.0           0.0   \n",
       "\n",
       "        vente_internet_max  purchase_date_min  purchase_date_max  \\\n",
       "0                      0.0         550.000000         550.000000   \n",
       "1                      0.0         550.000000         550.000000   \n",
       "2                      0.0         550.000000         550.000000   \n",
       "3                      0.0         550.000000         550.000000   \n",
       "4                      0.0         550.000000         550.000000   \n",
       "...                    ...                ...                ...   \n",
       "354360                 0.0         550.000000         550.000000   \n",
       "354361                 0.0         550.000000         550.000000   \n",
       "354362                 0.0          91.030556          91.020139   \n",
       "354363                 0.0          52.284028          52.284028   \n",
       "354364                 0.0         550.000000         550.000000   \n",
       "\n",
       "        time_between_purchase  nb_tickets_internet  ...  gender_female  \\\n",
       "0                   -1.000000                  0.0  ...              1   \n",
       "1                   -1.000000                  0.0  ...              0   \n",
       "2                   -1.000000                  0.0  ...              0   \n",
       "3                   -1.000000                  0.0  ...              0   \n",
       "4                   -1.000000                  0.0  ...              0   \n",
       "...                       ...                  ...  ...            ...   \n",
       "354360              -1.000000                  0.0  ...              0   \n",
       "354361              -1.000000                  0.0  ...              0   \n",
       "354362               0.010417                  0.0  ...              1   \n",
       "354363               0.000000                  0.0  ...              0   \n",
       "354364              -1.000000                  0.0  ...              0   \n",
       "\n",
       "        gender_male  gender_other  country_fr  nb_campaigns  \\\n",
       "0                 0             0         1.0          13.0   \n",
       "1                 0             1         1.0          10.0   \n",
       "2                 1             0         1.0          14.0   \n",
       "3                 0             1         NaN           9.0   \n",
       "4                 0             1         NaN           4.0   \n",
       "...             ...           ...         ...           ...   \n",
       "354360            0             1         NaN           7.0   \n",
       "354361            1             0         1.0          11.0   \n",
       "354362            0             0         1.0           6.0   \n",
       "354363            1             0         1.0           3.0   \n",
       "354364            1             0         NaN           7.0   \n",
       "\n",
       "        nb_campaigns_opened               time_to_open  y_has_purchased  \\\n",
       "0                       4.0            8 days 04:08:27              0.0   \n",
       "1                       9.0  0 days 01:39:58.555555555              0.0   \n",
       "2                       0.0                        NaN              0.0   \n",
       "3                       0.0                        NaN              0.0   \n",
       "4                       0.0                        NaN              0.0   \n",
       "...                     ...                        ...              ...   \n",
       "354360                  0.0                        NaN              0.0   \n",
       "354361                  2.0            3 days 06:21:17              0.0   \n",
       "354362                  6.0  0 days 17:30:10.166666666              1.0   \n",
       "354363                  0.0                        NaN              0.0   \n",
       "354364                  0.0                        NaN              0.0   \n",
       "\n",
       "        number_company  no_campaign_opened  \n",
       "0                   10               False  \n",
       "1                   10               False  \n",
       "2                   10                True  \n",
       "3                   10                True  \n",
       "4                   10                True  \n",
       "...                ...                 ...  \n",
       "354360              14                True  \n",
       "354361              14               False  \n",
       "354362              14               False  \n",
       "354363              14                True  \n",
       "354364              14                True  \n",
       "\n",
       "[354365 rows x 42 columns]"
      ]
     },
     "execution_count": 237,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# part de mails ouverts de chaque compagnie\n",
    "\n",
    "train_set_spectacle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 238,
   "id": "dc8cfd36-0eb2-4ef3-877d-626fd0a9ced4",
   "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>number_compagny</th>\n",
       "      <th>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "      <th>ratio_campaigns_opened</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>734772</td>\n",
       "      <td>126151.0</td>\n",
       "      <td>0.171687</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11</td>\n",
       "      <td>342396</td>\n",
       "      <td>129833.0</td>\n",
       "      <td>0.379190</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12</td>\n",
       "      <td>3168123</td>\n",
       "      <td>810722.0</td>\n",
       "      <td>0.255900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>13</td>\n",
       "      <td>3218569</td>\n",
       "      <td>793581.0</td>\n",
       "      <td>0.246563</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14</td>\n",
       "      <td>2427043</td>\n",
       "      <td>723846.0</td>\n",
       "      <td>0.298242</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number_compagny  nb_campaigns  nb_campaigns_opened  ratio_campaigns_opened\n",
       "0               10        734772             126151.0                0.171687\n",
       "1               11        342396             129833.0                0.379190\n",
       "2               12       3168123             810722.0                0.255900\n",
       "3               13       3218569             793581.0                0.246563\n",
       "4               14       2427043             723846.0                0.298242"
      ]
     },
     "execution_count": 238,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# taux d'ouverture des campaigns\n",
    "\n",
    "company_campaigns_stats = campaigns_information_spectacle.groupby(\"number_compagny\")[[\"nb_campaigns\", \"nb_campaigns_opened\"]].sum().reset_index()\n",
    "company_campaigns_stats[\"ratio_campaigns_opened\"] = company_campaigns_stats[\"nb_campaigns_opened\"]  / company_campaigns_stats[\"nb_campaigns\"]\n",
    "company_campaigns_stats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 239,
   "id": "30b28426-088a-4153-b2aa-c20f11b2b771",
   "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>number_company</th>\n",
       "      <th>y_has_purchased</th>\n",
       "      <th>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "      <th>perc_campaigns_opened</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>0.0</td>\n",
       "      <td>143960.0</td>\n",
       "      <td>18472.0</td>\n",
       "      <td>12.831342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10</td>\n",
       "      <td>1.0</td>\n",
       "      <td>10609.0</td>\n",
       "      <td>5177.0</td>\n",
       "      <td>48.798190</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>11</td>\n",
       "      <td>0.0</td>\n",
       "      <td>84676.0</td>\n",
       "      <td>27658.0</td>\n",
       "      <td>32.663328</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>11</td>\n",
       "      <td>1.0</td>\n",
       "      <td>20848.0</td>\n",
       "      <td>10927.0</td>\n",
       "      <td>52.412701</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>12</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>13</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1182992.0</td>\n",
       "      <td>275366.0</td>\n",
       "      <td>23.277080</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>13</td>\n",
       "      <td>1.0</td>\n",
       "      <td>107160.0</td>\n",
       "      <td>41244.0</td>\n",
       "      <td>38.488242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>14</td>\n",
       "      <td>0.0</td>\n",
       "      <td>822836.0</td>\n",
       "      <td>219220.0</td>\n",
       "      <td>26.642004</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>14</td>\n",
       "      <td>1.0</td>\n",
       "      <td>92099.0</td>\n",
       "      <td>34256.0</td>\n",
       "      <td>37.194758</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number_company  y_has_purchased  nb_campaigns  nb_campaigns_opened  \\\n",
       "0              10              0.0      143960.0              18472.0   \n",
       "1              10              1.0       10609.0               5177.0   \n",
       "2              11              0.0       84676.0              27658.0   \n",
       "3              11              1.0       20848.0              10927.0   \n",
       "4              12              0.0           0.0                  0.0   \n",
       "5              12              1.0           0.0                  0.0   \n",
       "6              13              0.0     1182992.0             275366.0   \n",
       "7              13              1.0      107160.0              41244.0   \n",
       "8              14              0.0      822836.0             219220.0   \n",
       "9              14              1.0       92099.0              34256.0   \n",
       "\n",
       "   perc_campaigns_opened  \n",
       "0              12.831342  \n",
       "1              48.798190  \n",
       "2              32.663328  \n",
       "3              52.412701  \n",
       "4                    NaN  \n",
       "5                    NaN  \n",
       "6              23.277080  \n",
       "7              38.488242  \n",
       "8              26.642004  \n",
       "9              37.194758  "
      ]
     },
     "execution_count": 239,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "company_campaigns_stats = train_set_spectacle.groupby([\"number_company\", \"y_has_purchased\"])[[\"nb_campaigns\", \"nb_campaigns_opened\"]].sum().reset_index()\n",
    "company_campaigns_stats[\"perc_campaigns_opened\"] = 100* (company_campaigns_stats[\"nb_campaigns_opened\"]  / company_campaigns_stats[\"nb_campaigns\"])\n",
    "company_campaigns_stats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 240,
   "id": "9cebe912-fce1-4f4f-9d87-9649605296c8",
   "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>number_company</th>\n",
       "      <th>y_has_purchased</th>\n",
       "      <th>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "      <th>perc_campaigns_opened</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>0.0</td>\n",
       "      <td>143960.0</td>\n",
       "      <td>18472.0</td>\n",
       "      <td>12.831342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10</td>\n",
       "      <td>1.0</td>\n",
       "      <td>10609.0</td>\n",
       "      <td>5177.0</td>\n",
       "      <td>48.798190</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>11</td>\n",
       "      <td>0.0</td>\n",
       "      <td>84676.0</td>\n",
       "      <td>27658.0</td>\n",
       "      <td>32.663328</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>11</td>\n",
       "      <td>1.0</td>\n",
       "      <td>20848.0</td>\n",
       "      <td>10927.0</td>\n",
       "      <td>52.412701</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>13</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1182992.0</td>\n",
       "      <td>275366.0</td>\n",
       "      <td>23.277080</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>13</td>\n",
       "      <td>1.0</td>\n",
       "      <td>107160.0</td>\n",
       "      <td>41244.0</td>\n",
       "      <td>38.488242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>14</td>\n",
       "      <td>0.0</td>\n",
       "      <td>822836.0</td>\n",
       "      <td>219220.0</td>\n",
       "      <td>26.642004</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>14</td>\n",
       "      <td>1.0</td>\n",
       "      <td>92099.0</td>\n",
       "      <td>34256.0</td>\n",
       "      <td>37.194758</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number_company  y_has_purchased  nb_campaigns  nb_campaigns_opened  \\\n",
       "0              10              0.0      143960.0              18472.0   \n",
       "1              10              1.0       10609.0               5177.0   \n",
       "2              11              0.0       84676.0              27658.0   \n",
       "3              11              1.0       20848.0              10927.0   \n",
       "6              13              0.0     1182992.0             275366.0   \n",
       "7              13              1.0      107160.0              41244.0   \n",
       "8              14              0.0      822836.0             219220.0   \n",
       "9              14              1.0       92099.0              34256.0   \n",
       "\n",
       "   perc_campaigns_opened  \n",
       "0              12.831342  \n",
       "1              48.798190  \n",
       "2              32.663328  \n",
       "3              52.412701  \n",
       "6              23.277080  \n",
       "7              38.488242  \n",
       "8              26.642004  \n",
       "9              37.194758  "
      ]
     },
     "execution_count": 240,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "company_campaigns_stats = company_campaigns_stats[company_campaigns_stats[\"number_company\"]!=12]\n",
    "company_campaigns_stats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 241,
   "id": "1c32cd86-e08d-4b8a-90f1-27ad0df0ffeb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 38328 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# graphic - overall rate of opened mails (train set for music companies)\n",
    "\n",
    "FILE_NAME = \"overall_mail_opening_train_set_music.png\"\n",
    "FILE_PATH_OUT_S3 = FILE_PATH + FILE_NAME\n",
    "\n",
    "multiple_barplot(company_campaigns_stats, x=\"number_company\", y=\"perc_campaigns_opened\", var_labels=\"y_has_purchased\",\n",
    "                dico_labels = {0 : \"clients n'ayant pas acheté\", 1 : \"clients ayant acheté sur la période\"},\n",
    "                xlabel = \"Compagnie\", ylabel = \"Part de mails ouverts (%)\", \n",
    "                title = \"Taux d'ouverture global des mails envoyés par les compagnies de spectacle (train set)\")\n",
    "\n",
    "# save in the s3\n",
    "\n",
    "with fs.open(FILE_PATH_OUT_S3, 'wb') as file_out:\n",
    "    plt.savefig(file_out)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "783f6fb2-5f26-42a9-a22d-f4ece44bfaf2",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### 3. products_purchased_reduced"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "74534ded-8121-43fb-8cf8-af353bed2c77",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nombre de lignes de la table :  764880\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "customer_id              0\n",
       "nb_tickets               0\n",
       "nb_purchases             0\n",
       "total_amount             0\n",
       "nb_suppliers             0\n",
       "vente_internet_max       0\n",
       "purchase_date_min        0\n",
       "purchase_date_max        0\n",
       "time_between_purchase    0\n",
       "nb_tickets_internet      0\n",
       "number_compagny          0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# nombre de nan\n",
    "print(\"Nombre de lignes de la table : \",products_purchased_reduced_spectacle.shape[0])\n",
    "products_purchased_reduced_spectacle.isna().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6db089d5-5517-4aee-a5fd-53f20ae3f0d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#importation librairies\n",
    "import warnings\n",
    "warnings.simplefilter(\"ignore\")\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import statsmodels\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from scipy.stats import shapiro\n",
    "from numpy.random import randn\n",
    "import scipy.stats as st\n",
    "%matplotlib inline\n",
    "\n",
    "#col_purchase=[\"nb_tickets\",\"nb_purchases\",\"total_amount\",\"nb_suppliers\",\"time_between_purchase\",\"nb_tickets_internet\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "943b8088-9ca2-40a4-b658-2cfae1589fac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30.0\n",
      "62.0\n",
      "120.0\n",
      "90.0\n",
      "Moustache inferieure -105.0\n",
      "Moustache superieure 255.0\n"
     ]
    }
   ],
   "source": [
    "#identification des valeur manquantes\n",
    "#calcule des quartile de la variable valeur(taille de la population)\n",
    "Q1=np.percentile(products_purchased_reduced_spectacle[\"total_amount\"], 25)  # Q1\n",
    "Q2=np.percentile(products_purchased_reduced_spectacle[\"total_amount\"], 50)  # Q2\n",
    "Q3=np.percentile(products_purchased_reduced_spectacle[\"total_amount\"], 75)  # Q3\n",
    "print(Q1)\n",
    "print(Q2)\n",
    "print(Q3)\n",
    "\n",
    "#intervale interquartile de la variable Valeur\n",
    "\n",
    "IQ=Q3-Q1\n",
    "print(IQ)\n",
    "\n",
    "#la valeur minimale des moustache de la variable Valeur\n",
    "\n",
    "M_inf=Q1-1.5*IQ\n",
    "M_sup=Q3+1.5*IQ\n",
    "\n",
    "print(\"Moustache inferieure\",M_inf)#moustache inferieur\n",
    "print(\"Moustache superieure\",M_sup)#moustache sup\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "c3adb0cd-8292-4c6f-9d4e-8352a6967022",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "customer_id                int64\n",
       "nb_tickets                 int64\n",
       "nb_purchases               int64\n",
       "total_amount             float64\n",
       "nb_suppliers               int64\n",
       "vente_internet_max         int64\n",
       "purchase_date_min        float64\n",
       "purchase_date_max        float64\n",
       "time_between_purchase    float64\n",
       "nb_tickets_internet      float64\n",
       "number_compagny            int64\n",
       "dtype: object"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "products_purchased_reduced_spectacle.dtypes"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a63e6d13-429b-4b01-ad11-27e5eea68cbd",
   "metadata": {},
   "source": [
    "#histogrames des variable quantitatives\n",
    "col_purchase=[\"nb_tickets\",\"nb_purchases\",\"total_amount\",\"nb_suppliers\",\"time_between_purchase\",\"nb_tickets_internet\"]\n",
    "for col in col_purchase:\n",
    "    plt.figure()\n",
    "    sns.histplot(products_purchased_reduced_spectacle[col], kde=True, color='red')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "5a08b5a5-7d56-4543-945a-38f6219d831d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27320 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Filtrer les données pour inclure uniquement les valeurs positives de total_amount et exclusion des valeur aberrantes\n",
    "filtered_products_purchased_reduced_spectacle = products_purchased_reduced_spectacle[(products_purchased_reduced_spectacle['total_amount'] > 0) & (products_purchased_reduced_spectacle['total_amount'] <= 255)]\n",
    "\n",
    "# Créer le graphique en utilisant les données filtrées\n",
    "sns.boxplot(data=filtered_data, y=\"total_amount\", x=\"number_compagny\", showfliers=False, showmeans=True)\n",
    "\n",
    "# Titre du graphique\n",
    "plt.title(\"Boite à moustache du chiffre d'affaire selon les compagnies de spectacles\")\n",
    "\n",
    "# Afficher le graphique\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "76e08ece-0b58-4b3a-abca-53e30ccc907b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Statistique F : 317.1792172580724\n",
      "Valeur de p : 3.665389608154993e-273\n",
      "Nombre de degrés de liberté entre les groupes : 4\n",
      "Nombre de degrés de liberté à l'intérieur des groupes : 670581\n",
      "Il y a des différences significatives entre au moins une des entrepries .\n"
     ]
    }
   ],
   "source": [
    "#test d'anova pour voir si la difference de chiffre d'affaire est statistiquement significative\n",
    "\n",
    "from scipy.stats import f_oneway\n",
    "\n",
    "# Créez une liste pour stocker les données de chaque groupe\n",
    "groupes = []\n",
    "\n",
    "# Parcourez chaque modalité de la variable catégorielle et divisez les données en groupes\n",
    "for modalite in filtered_products_purchased_reduced_spectacle['number_compagny'].unique():\n",
    "    groupe = filtered_products_purchased_reduced_spectacle[filtered_products_purchased_reduced_spectacle['number_compagny'] == modalite]['total_amount']\n",
    "    groupes.append(groupe)\n",
    "\n",
    "# Effectuez le test ANOVA\n",
    "f_statistic, p_value = f_oneway(*groupes)\n",
    "\n",
    "# Nombre total d'observations\n",
    "N = sum(len(groupe) for groupe in groupes)\n",
    "\n",
    "# Nombre de groupes ou de catégories\n",
    "k = len(groupes)\n",
    "\n",
    "# Degrés de liberté entre les groupes\n",
    "df_between = k - 1\n",
    "\n",
    "# Degrés de liberté à l'intérieur des groupes\n",
    "df_within = N - k\n",
    "\n",
    "# Affichez les résultats\n",
    "print(\"Statistique F :\", f_statistic)\n",
    "print(\"Valeur de p :\", p_value)\n",
    "\n",
    "print(\"Nombre de degrés de liberté entre les groupes :\", df_between)\n",
    "print(\"Nombre de degrés de liberté à l'intérieur des groupes :\", df_within)\n",
    "\n",
    "if p_value < 0.05:\n",
    "    print(\"Il y a des différences significatives entre au moins une des entrepries .\")\n",
    "else:\n",
    "    print(\"Il n'y a pas de différences significatives entre les entreprises .\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "9ec6e1c5-f3bc-4041-b32e-b62762246eb7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28308 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#repartition Chiffre d'affaire selon y_has_purchased\n",
    "\n",
    "# Filtrer les données pour inclure uniquement les valeurs positives de total_amount et exclusion des valeur aberrantes\n",
    "train_set_spectacle_filtered = train_set_spectacle[(train_set_spectacle['total_amount'] > 0) & (train_set_spectacle['total_amount'] <= 255)]\n",
    "\n",
    "# Créer le graphique en utilisant les données filtrées\n",
    "sns.boxplot(data=train_set_spectacle_filtered, y=\"total_amount\", x=\"y_has_purchased\", showfliers=False, showmeans=True)\n",
    "\n",
    "# Titre du graphique\n",
    "plt.title(\"Boite à moustache du chiffre d'affaire selon le statut d'achat du client\")\n",
    "\n",
    "# Afficher le graphique\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b55de4b-913e-4bc1-b4f2-cc0b1824d0e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#graphe sur le taux de ticket acheté"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "aacf2c34-f7ea-4d6e-935b-c5db01f03bbe",
   "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>number_compagny</th>\n",
       "      <th>nb_tickets</th>\n",
       "      <th>nb_tickets_internet</th>\n",
       "      <th>Taux_ticket_internet</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>492314</td>\n",
       "      <td>126262.0</td>\n",
       "      <td>25.646640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11</td>\n",
       "      <td>318969</td>\n",
       "      <td>16348.0</td>\n",
       "      <td>5.125263</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12</td>\n",
       "      <td>591028</td>\n",
       "      <td>42045.0</td>\n",
       "      <td>7.113876</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>13</td>\n",
       "      <td>7024227</td>\n",
       "      <td>1247482.0</td>\n",
       "      <td>17.759705</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14</td>\n",
       "      <td>335741</td>\n",
       "      <td>125638.0</td>\n",
       "      <td>37.421107</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number_compagny  nb_tickets  nb_tickets_internet  Taux_ticket_internet\n",
       "0               10      492314             126262.0             25.646640\n",
       "1               11      318969              16348.0              5.125263\n",
       "2               12      591028              42045.0              7.113876\n",
       "3               13     7024227            1247482.0             17.759705\n",
       "4               14      335741             125638.0             37.421107"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Taux de ticket payé par internet selon les compagnies\n",
    "\n",
    "purchase_spectacle = products_purchased_reduced_spectacle.groupby(\"number_compagny\")[[\"nb_tickets\", \"nb_tickets_internet\"]].sum().reset_index()\n",
    "purchase_spectacle[\"Taux_ticket_internet\"] = purchase_spectacle[\"nb_tickets_internet\"]*100 / purchase_spectacle[\"nb_tickets\"]\n",
    "purchase_spectacle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "f71bb53d-724b-454d-8743-305d20eec2b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28928 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Création du barplot\n",
    "plt.bar(purchase_spectacle[\"number_compagny\"],  purchase_spectacle[\"Taux_ticket_internet\"])\n",
    "\n",
    "# Ajout de titres et d'étiquettes\n",
    "plt.xlabel('Company')\n",
    "plt.ylabel(\"Taux d'achat de tickets en ligne (%)\")\n",
    "plt.title(\"Taux d'achat des tickets en ligne selon les compagnies de spectacle\")\n",
    "\n",
    "# Affichage du barplot\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "86fa4d7f-9b5f-4487-beb8-eb23771f724c",
   "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>number_company</th>\n",
       "      <th>y_has_purchased</th>\n",
       "      <th>nb_tickets</th>\n",
       "      <th>nb_tickets_internet</th>\n",
       "      <th>Taux_ticket_internet</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>0.0</td>\n",
       "      <td>9957.0</td>\n",
       "      <td>5450.0</td>\n",
       "      <td>54.735362</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10</td>\n",
       "      <td>1.0</td>\n",
       "      <td>7941.0</td>\n",
       "      <td>3424.0</td>\n",
       "      <td>43.117995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>11</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10361.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.048258</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>11</td>\n",
       "      <td>1.0</td>\n",
       "      <td>9638.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>35600.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>12</td>\n",
       "      <td>1.0</td>\n",
       "      <td>11520.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>13</td>\n",
       "      <td>0.0</td>\n",
       "      <td>131759.0</td>\n",
       "      <td>105406.0</td>\n",
       "      <td>79.999089</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>13</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1004076.0</td>\n",
       "      <td>13902.0</td>\n",
       "      <td>1.384557</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>14</td>\n",
       "      <td>0.0</td>\n",
       "      <td>44596.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>14</td>\n",
       "      <td>1.0</td>\n",
       "      <td>16694.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number_company  y_has_purchased  nb_tickets  nb_tickets_internet  \\\n",
       "0              10              0.0      9957.0               5450.0   \n",
       "1              10              1.0      7941.0               3424.0   \n",
       "2              11              0.0     10361.0                  5.0   \n",
       "3              11              1.0      9638.0                  0.0   \n",
       "4              12              0.0     35600.0                  0.0   \n",
       "5              12              1.0     11520.0                  0.0   \n",
       "6              13              0.0    131759.0             105406.0   \n",
       "7              13              1.0   1004076.0              13902.0   \n",
       "8              14              0.0     44596.0                  0.0   \n",
       "9              14              1.0     16694.0                  0.0   \n",
       "\n",
       "   Taux_ticket_internet  \n",
       "0             54.735362  \n",
       "1             43.117995  \n",
       "2              0.048258  \n",
       "3              0.000000  \n",
       "4              0.000000  \n",
       "5              0.000000  \n",
       "6             79.999089  \n",
       "7              1.384557  \n",
       "8              0.000000  \n",
       "9              0.000000  "
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Taux de ticket payé  en ligne selon y_has_purchase par  compagnies avec la base de train\n",
    "\n",
    "purchase_spectacle_train = train_set_spectacle.groupby([\"number_company\", \"y_has_purchased\"])[[\"nb_tickets\", \"nb_tickets_internet\"]].sum().reset_index()\n",
    "purchase_spectacle_train[\"Taux_ticket_internet\"] = purchase_spectacle_train[\"nb_tickets_internet\"]*100 / purchase_spectacle_train[\"nb_tickets\"]\n",
    "purchase_spectacle_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "d11335b7-e35a-44c7-8ce4-661216978151",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 46304 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "multiple_barplot(purchase_spectacle_train, x=\"number_company\", y=\"Taux_ticket_internet\", var_labels=\"y_has_purchased\",\n",
    "                dico_labels = {0 : \"clients n'ayant pas acheté\", 1 : \"clients ayant acheté sur la période\"},\n",
    "                xlabel = \"Numéro de compagnie\", ylabel = \"Taux de ticket acheté par internet (%)\", \n",
    "                title = \"Taux de ticket achété en ligne  selon y_has_purchased par  compagnies de spectacle (train set)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "f8444cab-d4c5-4afd-b472-476e702c09cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 24900 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import seaborn as sns\n",
    "\n",
    "\n",
    "# Créer le graphique à barres\n",
    "sns.barplot(data=purchase_spectacle_train, x=\"y_has_purchased\", y=\"Taux_ticket_internet\",ci=None)\n",
    "\n",
    "\n",
    "# Titre du graphique\n",
    "plt.title(\"Taux moyen de tickets achetés selon le statut d'achat du client\")\n",
    "\n",
    "# Ajouter une étiquette à l'axe des abscisses\n",
    "plt.xlabel(\"Statut d'achat du client\")\n",
    "\n",
    "# Ajouter une étiquette à l'axe des ordonnées\n",
    "plt.ylabel(\"Taux de tickets internet\")\n",
    "\n",
    "# Afficher le graphique\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "9ba02de7-3087-4b0c-884a-dc4a6ca92c3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#stat sur la variable temps ecoulé entre le premier et le dernier achat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "59a95248-0261-4970-9e91-e43d50cf4d69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Boite à moustache du temps ecoulés entre le premier et le dernier achat selon les compagnies de spectacles')"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 38360 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#repartition des client selon le temps ecoulés entre le premier et le denier achat par  compagnie\n",
    "\n",
    "sns.boxplot(data=products_purchased_reduced_spectacle, y=\"time_between_purchase\",x=\"number_compagny\",showfliers=False,showmeans=True)\n",
    "plt.title(\"Boite à moustache du temps ecoulés entre le premier et le dernier achat selon les compagnies de spectacles\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "e2c51e28-6197-48f0-ab6d-9fc7b3b0de74",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Statistique F : 7956.05932109542\n",
      "Valeur de p : 0.0\n",
      "Nombre de degrés de liberté entre les groupes : 4\n",
      "Nombre de degrés de liberté à l'intérieur des groupes : 764875\n",
      "Il y a des différences significatives entre au moins une des entrepries .\n"
     ]
    }
   ],
   "source": [
    "#test d'anova pour voir si la difference de temps entre le premier et le dernier achat est statistiquement significative\n",
    "\n",
    "from scipy.stats import f_oneway\n",
    "\n",
    "# Créez une liste pour stocker les données de chaque groupe\n",
    "groupes = []\n",
    "\n",
    "# Parcourez chaque modalité de la variable catégorielle et divisez les données en groupes\n",
    "for modalite in products_purchased_reduced_spectacle['number_compagny'].unique():\n",
    "    groupe = products_purchased_reduced_spectacle[products_purchased_reduced_spectacle['number_compagny'] == modalite]['time_between_purchase']\n",
    "    groupes.append(groupe)\n",
    "\n",
    "# Effectuez le test ANOVA\n",
    "f_statistic, p_value = f_oneway(*groupes)\n",
    "\n",
    "# Nombre total d'observations\n",
    "N = sum(len(groupe) for groupe in groupes)\n",
    "\n",
    "# Nombre de groupes ou de catégories\n",
    "k = len(groupes)\n",
    "\n",
    "# Degrés de liberté entre les groupes\n",
    "df_between = k - 1\n",
    "\n",
    "# Degrés de liberté à l'intérieur des groupes\n",
    "df_within = N - k\n",
    "\n",
    "# Affichez les résultats\n",
    "print(\"Statistique F :\", f_statistic)\n",
    "print(\"Valeur de p :\", p_value)\n",
    "\n",
    "print(\"Nombre de degrés de liberté entre les groupes :\", df_between)\n",
    "print(\"Nombre de degrés de liberté à l'intérieur des groupes :\", df_within)\n",
    "\n",
    "if p_value < 0.05:\n",
    "    print(\"Il y a des différences significatives entre au moins une des entrepries .\")\n",
    "else:\n",
    "    print(\"Il n'y a pas de différences significatives entre les entreprises .\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "75a003ab-f42a-4b2d-a0a8-284e673e71f7",
   "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>number_company</th>\n",
       "      <th>y_has_purchased</th>\n",
       "      <th>time_between_purchase</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>0.0</td>\n",
       "      <td>45.791114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10</td>\n",
       "      <td>1.0</td>\n",
       "      <td>193.080793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>11</td>\n",
       "      <td>0.0</td>\n",
       "      <td>27.640469</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>11</td>\n",
       "      <td>1.0</td>\n",
       "      <td>129.853892</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>16.418446</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>12</td>\n",
       "      <td>1.0</td>\n",
       "      <td>58.548598</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>13</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10.012525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>13</td>\n",
       "      <td>1.0</td>\n",
       "      <td>93.545373</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>14</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.879196</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>14</td>\n",
       "      <td>1.0</td>\n",
       "      <td>10.745213</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number_company  y_has_purchased  time_between_purchase\n",
       "0              10              0.0              45.791114\n",
       "1              10              1.0             193.080793\n",
       "2              11              0.0              27.640469\n",
       "3              11              1.0             129.853892\n",
       "4              12              0.0              16.418446\n",
       "5              12              1.0              58.548598\n",
       "6              13              0.0              10.012525\n",
       "7              13              1.0              93.545373\n",
       "8              14              0.0               3.879196\n",
       "9              14              1.0              10.745213"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#repartition des client selon le temps ecoulés entre le premier et le denier achat par  compagnie\n",
    "purchase_train_time= train_set_spectacle.groupby([\"number_company\", \"y_has_purchased\"])[\"time_between_purchase\"].mean().reset_index()\n",
    "purchase_train_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "f27921a9-1253-4c02-9bff-8cd3c4a9a5d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 48624 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "multiple_barplot(purchase_train_time, x=\"number_company\", y=\"time_between_purchase\", var_labels=\"y_has_purchased\",\n",
    "                dico_labels = {0 : \"clients n'ayant pas acheté\", 1 : \"clients ayant acheté sur la période\"},\n",
    "                xlabel = \"Numéro de compagnie\", ylabel = \"Taux de ticket acheté par internet (%)\", \n",
    "                title = \"temps moyen entre le premier et le dernier achat selon y_has_purchased par  compagnies de spectacle (train set)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "74f06e96-3c25-4eca-8190-25b0a4ab0d75",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "customer_id                int64\n",
       "nb_tickets                 int64\n",
       "nb_purchases               int64\n",
       "total_amount             float64\n",
       "nb_suppliers               int64\n",
       "vente_internet_max         int64\n",
       "purchase_date_min        float64\n",
       "purchase_date_max        float64\n",
       "time_between_purchase    float64\n",
       "nb_tickets_internet      float64\n",
       "number_compagny            int64\n",
       "dtype: object"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "products_purchased_reduced_spectacle.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "aa6655c0-c602-4485-8b38-3117227464e1",
   "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_tickets</th>\n",
       "      <th>nb_purchases</th>\n",
       "      <th>total_amount</th>\n",
       "      <th>nb_suppliers</th>\n",
       "      <th>vente_internet_max</th>\n",
       "      <th>purchase_date_min</th>\n",
       "      <th>purchase_date_max</th>\n",
       "      <th>time_between_purchase</th>\n",
       "      <th>nb_tickets_internet</th>\n",
       "      <th>number_compagny</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>19482</td>\n",
       "      <td>88</td>\n",
       "      <td>29</td>\n",
       "      <td>872.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2643.092500</td>\n",
       "      <td>718.149398</td>\n",
       "      <td>1924.943102</td>\n",
       "      <td>8.0</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>19484</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>62.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1745.021736</td>\n",
       "      <td>1743.045035</td>\n",
       "      <td>1.976701</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>19485</td>\n",
       "      <td>131</td>\n",
       "      <td>21</td>\n",
       "      <td>1878.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2649.044745</td>\n",
       "      <td>85.240845</td>\n",
       "      <td>2563.803900</td>\n",
       "      <td>84.0</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>19486</td>\n",
       "      <td>10</td>\n",
       "      <td>4</td>\n",
       "      <td>96.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1944.077604</td>\n",
       "      <td>1742.794225</td>\n",
       "      <td>201.283380</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>19487</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>33.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1742.877766</td>\n",
       "      <td>1742.877766</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10</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",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99580</th>\n",
       "      <td>6884747</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>40.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.193750</td>\n",
       "      <td>0.193750</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99581</th>\n",
       "      <td>6884748</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>40.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.186806</td>\n",
       "      <td>0.186806</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99582</th>\n",
       "      <td>6884750</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>80.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.136111</td>\n",
       "      <td>0.136111</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99583</th>\n",
       "      <td>6884751</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>40.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.122917</td>\n",
       "      <td>0.122917</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99584</th>\n",
       "      <td>6884753</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>40.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.047222</td>\n",
       "      <td>0.047222</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>764880 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       customer_id  nb_tickets  nb_purchases  total_amount  nb_suppliers  \\\n",
       "0            19482          88            29         872.0             2   \n",
       "1            19484           3             2          62.0             1   \n",
       "2            19485         131            21        1878.0             2   \n",
       "3            19486          10             4          96.0             1   \n",
       "4            19487           2             1          33.0             1   \n",
       "...            ...         ...           ...           ...           ...   \n",
       "99580      6884747           2             1          40.0             1   \n",
       "99581      6884748           2             1          40.0             1   \n",
       "99582      6884750           4             1          80.0             1   \n",
       "99583      6884751           2             1          40.0             1   \n",
       "99584      6884753           2             1          40.0             1   \n",
       "\n",
       "       vente_internet_max  purchase_date_min  purchase_date_max  \\\n",
       "0                       1        2643.092500         718.149398   \n",
       "1                       0        1745.021736        1743.045035   \n",
       "2                       1        2649.044745          85.240845   \n",
       "3                       0        1944.077604        1742.794225   \n",
       "4                       0        1742.877766        1742.877766   \n",
       "...                   ...                ...                ...   \n",
       "99580                   0           0.193750           0.193750   \n",
       "99581                   0           0.186806           0.186806   \n",
       "99582                   0           0.136111           0.136111   \n",
       "99583                   0           0.122917           0.122917   \n",
       "99584                   0           0.047222           0.047222   \n",
       "\n",
       "       time_between_purchase  nb_tickets_internet  number_compagny  \n",
       "0                1924.943102                  8.0               10  \n",
       "1                   1.976701                  0.0               10  \n",
       "2                2563.803900                 84.0               10  \n",
       "3                 201.283380                  0.0               10  \n",
       "4                   0.000000                  0.0               10  \n",
       "...                      ...                  ...              ...  \n",
       "99580               0.000000                  0.0               14  \n",
       "99581               0.000000                  0.0               14  \n",
       "99582               0.000000                  0.0               14  \n",
       "99583               0.000000                  0.0               14  \n",
       "99584               0.000000                  0.0               14  \n",
       "\n",
       "[764880 rows x 11 columns]"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "products_purchased_reduced_spectacle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be04e2f9-60b9-4b44-ab36-06a365b21e32",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Stat sur les canaux de vente"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "20a70ec0-38f6-470e-a442-7884a150613a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28252 characters]",
      "text/plain": [
       "<Figure size 800x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#Repartition du nombre de canaux de vente selon les entreprise\n",
    "\n",
    "# Filtrer les données pour inclure uniquement les valeurs positives de total_amount et exclusion des valeur aberrantes\n",
    "purchase_canaux = products_purchased_reduced_spectacle[(products_purchased_reduced_spectacle['nb_tickets'] > 0) ]\n",
    "\n",
    "plt.figure(figsize=(8, 6))\n",
    "sns.barplot(x='number_compagny', y='nb_suppliers', data=purchase_canaux, ci=None)  # ci=None pour ne pas afficher les intervalles de confiance\n",
    "plt.title('Nombre moyen de canaux de vente par entreprise')\n",
    "plt.xlabel('number_compagny')\n",
    "plt.ylabel('Nombre moyen de caneaux ')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "ee901539-37d1-4dfa-8e78-38e4947c3d35",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    697297.000000\n",
       "mean          0.110917\n",
       "std           0.319561\n",
       "min           0.000000\n",
       "25%           0.000000\n",
       "50%           0.000000\n",
       "75%           0.000000\n",
       "max           8.000000\n",
       "Name: nb_suppliers, dtype: float64"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_set_spectacle[\"nb_suppliers\"].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "7389053e-54ae-4167-9afd-aa5d194822ef",
   "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>number_company</th>\n",
       "      <th>y_has_purchased</th>\n",
       "      <th>nb_suppliers</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.118250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.340136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>11</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.033992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>11</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.155239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>12</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.153296</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>12</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.220174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>13</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.007711</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>13</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.083750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>14</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>14</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number_company  y_has_purchased  nb_suppliers\n",
       "0              10              0.0      1.118250\n",
       "1              10              1.0      1.340136\n",
       "2              11              0.0      1.033992\n",
       "3              11              1.0      1.155239\n",
       "4              12              0.0      0.153296\n",
       "5              12              1.0      0.220174\n",
       "6              13              0.0      1.007711\n",
       "7              13              1.0      1.083750\n",
       "8              14              0.0      1.000000\n",
       "9              14              1.0      1.000000"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#repartition des client selon le nombre moyen de canaux utilisé pour l'achat de ticket  par  compagnie sur base de train\n",
    "\n",
    "#purchase_train_canaux = train_set_spectacle[(train_set_spectacle['nb_tickets'] > 0) ]\n",
    "\n",
    "purchase_train_canaux_filtered= purchase_train_canaux.groupby([\"number_company\", \"y_has_purchased\"])[\"nb_suppliers\"].mean().reset_index()\n",
    "purchase_train_canaux_filtered"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "e4079e46-db8b-4a25-9da6-37b1405c57d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 45552 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "multiple_barplot(purchase_train_canaux_filtered, x=\"number_company\", y=\"nb_suppliers\", var_labels=\"y_has_purchased\",\n",
    "                dico_labels = {0 : \"clients n'ayant pas acheté\", 1 : \"clients ayant acheté sur la période\"},\n",
    "                xlabel = \"Numéro de compagnie\", ylabel = \"Nombre moyen de canaux d'achat\", \n",
    "                title = \"Nombre moyen de canaux d'acht selon y_has_purchased par  compagnies de spectacle (train set)\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9e84af4-a02b-4f83-81ae-b7a73475d060",
   "metadata": {},
   "source": [
    "### 4. target_information"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2867eceb-1f72-406c-adc2-adfedcaf60e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nombre de lignes de la table :  6240166\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "id                       0\n",
       "customer_id              0\n",
       "target_name              0\n",
       "target_type_is_import    0\n",
       "target_type_name         0\n",
       "number_compagny          0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# nombre de nan\n",
    "print(\"Nombre de lignes de la table : \",target_information_spectacle.shape[0])\n",
    "target_information_spectacle.isna().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "561f361d-7d39-430a-9e27-a32f6c2f7b50",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pas exploitable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "904cbf32-77b6-49dd-a96c-9e7e5a0175c3",
   "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
}
