{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3f41343f-7205-41d9-89dd-88039e301413",
   "metadata": {},
   "source": [
    "# Statistiques descriptives"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "abfaf341-7b35-4407-9133-d21336c04027",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os\n",
    "import s3fs\n",
    "import re\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.dates as mdates\n",
    "from datetime import datetime, date, timedelta\n",
    "from dateutil.relativedelta import relativedelta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7fb72fa3-7940-496f-ac78-c2837f65eefa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create filesystem object\n",
    "S3_ENDPOINT_URL = \"https://\" + os.environ[\"AWS_S3_ENDPOINT\"]\n",
    "fs = s3fs.S3FileSystem(client_kwargs={'endpoint_url': S3_ENDPOINT_URL})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45d5261f-4d46-49cb-8582-dd2121122b05",
   "metadata": {},
   "source": [
    "# 1 - Comportement d'achat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3479960c-0d23-45f1-8fff-d87395205731",
   "metadata": {},
   "source": [
    "## Outlier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9376af51-4320-44b6-8f30-1e1234371556",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Chargement des données temporaires\n",
    "BUCKET = \"projet-bdc2324-team1\"\n",
    "FILE_KEY_S3 = \"0_Temp/Company 1 - Purchasing behaviour.csv\"\n",
    "FILE_PATH_S3 = BUCKET + \"/\" + FILE_KEY_S3\n",
    "\n",
    "with fs.open(FILE_PATH_S3, mode=\"rb\") as file_in:\n",
    "    tickets_kpi = pd.read_csv(file_in, sep=\",\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1855dcca-cfce-4c54-90ae-55d9a1ab5d45",
   "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>event_type_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>name_event_types</th>\n",
       "      <th>avg_amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>384226</td>\n",
       "      <td>194790</td>\n",
       "      <td>2686540.5</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>3262.190868</td>\n",
       "      <td>4.179306</td>\n",
       "      <td>3258.011562</td>\n",
       "      <td>51.0</td>\n",
       "      <td>offre muséale individuel</td>\n",
       "      <td>6.150659</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>453242</td>\n",
       "      <td>228945</td>\n",
       "      <td>3248965.5</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>3698.198229</td>\n",
       "      <td>5.221840</td>\n",
       "      <td>3692.976389</td>\n",
       "      <td>2988.0</td>\n",
       "      <td>spectacle vivant</td>\n",
       "      <td>7.762474</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>201750</td>\n",
       "      <td>107110</td>\n",
       "      <td>1459190.0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>3803.369792</td>\n",
       "      <td>0.146331</td>\n",
       "      <td>3803.223461</td>\n",
       "      <td>9.0</td>\n",
       "      <td>offre muséale groupe</td>\n",
       "      <td>4.452618</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>217356</td>\n",
       "      <td>111786</td>\n",
       "      <td>1435871.5</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2502.715509</td>\n",
       "      <td>1408.715532</td>\n",
       "      <td>1093.999977</td>\n",
       "      <td>5.0</td>\n",
       "      <td>formule adhésion</td>\n",
       "      <td>6.439463</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>143</td>\n",
       "      <td>143</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2041.274549</td>\n",
       "      <td>1340.308160</td>\n",
       "      <td>700.966389</td>\n",
       "      <td>0.0</td>\n",
       "      <td>offre muséale individuel</td>\n",
       "      <td>6.150659</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   customer_id  event_type_id  nb_tickets  nb_purchases  total_amount  \\\n",
       "0            1              2      384226        194790     2686540.5   \n",
       "1            1              4      453242        228945     3248965.5   \n",
       "2            1              5      201750        107110     1459190.0   \n",
       "3            1              6      217356        111786     1435871.5   \n",
       "4            2              2         143           143           0.0   \n",
       "\n",
       "   nb_suppliers  vente_internet_max  purchase_date_min  purchase_date_max  \\\n",
       "0             7                   1        3262.190868           4.179306   \n",
       "1             6                   1        3698.198229           5.221840   \n",
       "2             6                   1        3803.369792           0.146331   \n",
       "3             5                   1        2502.715509        1408.715532   \n",
       "4             1                   0        2041.274549        1340.308160   \n",
       "\n",
       "   time_between_purchase  nb_tickets_internet          name_event_types  \\\n",
       "0            3258.011562                 51.0  offre muséale individuel   \n",
       "1            3692.976389               2988.0          spectacle vivant   \n",
       "2            3803.223461                  9.0      offre muséale groupe   \n",
       "3            1093.999977                  5.0          formule adhésion   \n",
       "4             700.966389                  0.0  offre muséale individuel   \n",
       "\n",
       "   avg_amount  \n",
       "0    6.150659  \n",
       "1    7.762474  \n",
       "2    4.452618  \n",
       "3    6.439463  \n",
       "4    6.150659  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tickets_kpi.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0e5d3b2e-1a75-4d46-80e6-c306e9f8de84",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['customer_id', 'event_type_id', 'nb_tickets', 'nb_purchases',\n",
       "       'total_amount', 'nb_suppliers', 'vente_internet_max',\n",
       "       'purchase_date_min', 'purchase_date_max', 'time_between_purchase',\n",
       "       'nb_tickets_internet', 'name_event_types', 'avg_amount'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tickets_kpi.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7667e8eb-9a1e-4216-96f4-bf987c6e30b5",
   "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>event_type_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>name_event_types</th>\n",
       "      <th>avg_amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>453242</td>\n",
       "      <td>228945</td>\n",
       "      <td>3248965.5</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>3698.198229</td>\n",
       "      <td>5.221840</td>\n",
       "      <td>3692.976389</td>\n",
       "      <td>2988.0</td>\n",
       "      <td>spectacle vivant</td>\n",
       "      <td>7.762474</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>384226</td>\n",
       "      <td>194790</td>\n",
       "      <td>2686540.5</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>3262.190868</td>\n",
       "      <td>4.179306</td>\n",
       "      <td>3258.011562</td>\n",
       "      <td>51.0</td>\n",
       "      <td>offre muséale individuel</td>\n",
       "      <td>6.150659</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>217356</td>\n",
       "      <td>111786</td>\n",
       "      <td>1435871.5</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2502.715509</td>\n",
       "      <td>1408.715532</td>\n",
       "      <td>1093.999977</td>\n",
       "      <td>5.0</td>\n",
       "      <td>formule adhésion</td>\n",
       "      <td>6.439463</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>201750</td>\n",
       "      <td>107110</td>\n",
       "      <td>1459190.0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>3803.369792</td>\n",
       "      <td>0.146331</td>\n",
       "      <td>3803.223461</td>\n",
       "      <td>9.0</td>\n",
       "      <td>offre muséale groupe</td>\n",
       "      <td>4.452618</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5032</th>\n",
       "      <td>6733</td>\n",
       "      <td>6</td>\n",
       "      <td>14208</td>\n",
       "      <td>114</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2492.187199</td>\n",
       "      <td>1442.405116</td>\n",
       "      <td>1049.782083</td>\n",
       "      <td>13497.0</td>\n",
       "      <td>formule adhésion</td>\n",
       "      <td>6.439463</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5029</th>\n",
       "      <td>6733</td>\n",
       "      <td>2</td>\n",
       "      <td>11656</td>\n",
       "      <td>158</td>\n",
       "      <td>471.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2982.237384</td>\n",
       "      <td>489.495324</td>\n",
       "      <td>2492.742060</td>\n",
       "      <td>9815.0</td>\n",
       "      <td>offre muséale individuel</td>\n",
       "      <td>6.150659</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5030</th>\n",
       "      <td>6733</td>\n",
       "      <td>4</td>\n",
       "      <td>7440</td>\n",
       "      <td>162</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1036.392674</td>\n",
       "      <td>426.201944</td>\n",
       "      <td>610.190729</td>\n",
       "      <td>7419.0</td>\n",
       "      <td>spectacle vivant</td>\n",
       "      <td>7.762474</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>60</th>\n",
       "      <td>41</td>\n",
       "      <td>6</td>\n",
       "      <td>6583</td>\n",
       "      <td>634</td>\n",
       "      <td>12546.5</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2501.337905</td>\n",
       "      <td>1409.370521</td>\n",
       "      <td>1091.967384</td>\n",
       "      <td>6391.0</td>\n",
       "      <td>formule adhésion</td>\n",
       "      <td>6.439463</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>57</th>\n",
       "      <td>41</td>\n",
       "      <td>2</td>\n",
       "      <td>6514</td>\n",
       "      <td>812</td>\n",
       "      <td>22423.0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>3576.106609</td>\n",
       "      <td>247.232697</td>\n",
       "      <td>3328.873912</td>\n",
       "      <td>5321.0</td>\n",
       "      <td>offre muséale individuel</td>\n",
       "      <td>6.150659</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36376</th>\n",
       "      <td>63488</td>\n",
       "      <td>4</td>\n",
       "      <td>5750</td>\n",
       "      <td>9</td>\n",
       "      <td>63250.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>887.298484</td>\n",
       "      <td>440.265162</td>\n",
       "      <td>447.033322</td>\n",
       "      <td>5750.0</td>\n",
       "      <td>spectacle vivant</td>\n",
       "      <td>7.762474</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       customer_id  event_type_id  nb_tickets  nb_purchases  total_amount  \\\n",
       "1                1              4      453242        228945     3248965.5   \n",
       "0                1              2      384226        194790     2686540.5   \n",
       "3                1              6      217356        111786     1435871.5   \n",
       "2                1              5      201750        107110     1459190.0   \n",
       "5032          6733              6       14208           114           0.0   \n",
       "5029          6733              2       11656           158         471.0   \n",
       "5030          6733              4        7440           162           0.0   \n",
       "60              41              6        6583           634       12546.5   \n",
       "57              41              2        6514           812       22423.0   \n",
       "36376        63488              4        5750             9       63250.0   \n",
       "\n",
       "       nb_suppliers  vente_internet_max  purchase_date_min  purchase_date_max  \\\n",
       "1                 6                   1        3698.198229           5.221840   \n",
       "0                 7                   1        3262.190868           4.179306   \n",
       "3                 5                   1        2502.715509        1408.715532   \n",
       "2                 6                   1        3803.369792           0.146331   \n",
       "5032              3                   1        2492.187199        1442.405116   \n",
       "5029              3                   1        2982.237384         489.495324   \n",
       "5030              2                   1        1036.392674         426.201944   \n",
       "60                4                   1        2501.337905        1409.370521   \n",
       "57                6                   1        3576.106609         247.232697   \n",
       "36376             1                   1         887.298484         440.265162   \n",
       "\n",
       "       time_between_purchase  nb_tickets_internet          name_event_types  \\\n",
       "1                3692.976389               2988.0          spectacle vivant   \n",
       "0                3258.011562                 51.0  offre muséale individuel   \n",
       "3                1093.999977                  5.0          formule adhésion   \n",
       "2                3803.223461                  9.0      offre muséale groupe   \n",
       "5032             1049.782083              13497.0          formule adhésion   \n",
       "5029             2492.742060               9815.0  offre muséale individuel   \n",
       "5030              610.190729               7419.0          spectacle vivant   \n",
       "60               1091.967384               6391.0          formule adhésion   \n",
       "57               3328.873912               5321.0  offre muséale individuel   \n",
       "36376             447.033322               5750.0          spectacle vivant   \n",
       "\n",
       "       avg_amount  \n",
       "1        7.762474  \n",
       "0        6.150659  \n",
       "3        6.439463  \n",
       "2        4.452618  \n",
       "5032     6.439463  \n",
       "5029     6.150659  \n",
       "5030     7.762474  \n",
       "60       6.439463  \n",
       "57       6.150659  \n",
       "36376    7.762474  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Présence d'outlier\n",
    "tickets_kpi.sort_values(by = ['nb_tickets'], axis = 0, ascending = False).head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9b2e27f2-703d-465b-a0f9-76e996de617c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Part du CA par customer\n",
    "total_amount_share = 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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "36141803-8865-4210-bd39-0a980301fd0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 19036 characters]",
      "text/plain": [
       "<Figure size 300x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Costumer 1 vs others customers\n",
    "coupure = 1\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": 9,
   "id": "94cf1a25-9ded-48f2-b1b2-75225bdaf49d",
   "metadata": {},
   "outputs": [],
   "source": [
    "tickets_kpi_filtered  = tickets_kpi[tickets_kpi['customer_id'] != 1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbebfa92-310a-417b-a7fa-36ac3593db06",
   "metadata": {},
   "source": [
    "## Evolution des commandes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "06137694-7f50-47ba-8749-68471ececc1e",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_1235/3643128924.py:11: 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",
      "  purchases = pd.read_csv(file_in, sep=\",\", parse_dates = ['purchase_date'], date_parser=custom_date_parser)\n",
      "/tmp/ipykernel_1235/3643128924.py:19: 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",
      "  campaigns = pd.read_csv(file_in, sep=\",\", parse_dates = ['sent_at'], date_parser=custom_date_parser)\n"
     ]
    }
   ],
   "source": [
    "# Importation - Chargement des données temporaires\n",
    "def custom_date_parser(date_string):\n",
    "    return pd.to_datetime(date_string, utc = True, format = 'ISO8601')\n",
    "\n",
    "# Achat\n",
    "BUCKET = \"projet-bdc2324-team1\"\n",
    "FILE_KEY_S3 = \"0_Input/Company_1/products_purchased_reduced.csv\"\n",
    "FILE_PATH_S3 = BUCKET + \"/\" + FILE_KEY_S3\n",
    "\n",
    "with fs.open(FILE_PATH_S3, mode=\"rb\") as file_in:\n",
    "    purchases = pd.read_csv(file_in, sep=\",\", parse_dates = ['purchase_date'], date_parser=custom_date_parser)\n",
    "    \n",
    "# Emails\n",
    "BUCKET = \"projet-bdc2324-team1\"\n",
    "FILE_KEY_S3 = \"0_Input/Company_1/campaigns_information.csv\"\n",
    "FILE_PATH_S3 = BUCKET + \"/\" + FILE_KEY_S3\n",
    "\n",
    "with fs.open(FILE_PATH_S3, mode=\"rb\") as file_in:\n",
    "    campaigns = pd.read_csv(file_in, sep=\",\", parse_dates = ['sent_at'], date_parser=custom_date_parser)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e6b962d4-1a30-4133-ac0f-359f7afef42c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Mois du premier achat\n",
    "purchase_min = purchases.groupby(['customer_id'])['purchase_date'].min().reset_index()\n",
    "purchase_min.rename(columns = {'purchase_date' : 'first_purchase_event'}, inplace = True)\n",
    "purchase_min['first_purchase_event'] = pd.to_datetime(purchase_min['first_purchase_event'])\n",
    "purchase_min['first_purchase_month'] = pd.to_datetime(purchase_min['first_purchase_event'].dt.strftime('%Y-%m'))\n",
    "\n",
    "# Mois du premier mails\n",
    "first_mail_received = campaigns.groupby('customer_id')['sent_at'].min().reset_index()\n",
    "first_mail_received.rename(columns = {'sent_at' : 'first_email_reception'}, inplace = True)\n",
    "first_mail_received['first_email_reception'] = pd.to_datetime(first_mail_received['first_email_reception'])\n",
    "first_mail_received['first_email_month'] = pd.to_datetime(first_mail_received['first_email_reception'].dt.strftime('%Y-%m'))\n",
    "\n",
    "# Fusion \n",
    "known_customer = pd.merge(purchase_min[['customer_id', 'first_purchase_month']], \n",
    "                          first_mail_received[['customer_id', 'first_email_month']], on = 'customer_id', how = 'outer')\n",
    "\n",
    "# Mois à partir duquel le client est considere comme connu\n",
    "known_customer['known_date'] = pd.to_datetime(known_customer[['first_email_month', 'first_purchase_month']].min(axis = 1), utc = True, format = 'ISO8601')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9c56e5ac-cbf4-4343-80ba-be2ab8b60eab",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Nombre de commande par mois\n",
    "purchases_count = pd.merge(purchases[['customer_id', 'purchase_id', 'purchase_date']].drop_duplicates(), known_customer[['customer_id', 'known_date']], on = ['customer_id'], how = 'inner')\n",
    "purchases_count['is_customer_known'] = purchases_count['purchase_date'] > purchases_count['known_date'] + pd.DateOffset(months=1)\n",
    "purchases_count['purchase_date_month'] = pd.to_datetime(purchases_count['purchase_date'].dt.strftime('%Y-%m'))\n",
    "purchases_count = purchases_count[purchases_count['customer_id'] != 1]\n",
    "\n",
    "# Nombre de commande par mois par type de client\n",
    "nb_purchases_graph = purchases_count.groupby(['purchase_date_month', 'is_customer_known'])['purchase_id'].count().reset_index()\n",
    "nb_purchases_graph.rename(columns = {'purchase_id' : 'nb_purchases'}, inplace = True)\n",
    "\n",
    "nb_purchases_graph_2 = purchases_count.groupby(['purchase_date_month', 'is_customer_known'])['customer_id'].nunique().reset_index()\n",
    "nb_purchases_graph_2.rename(columns = {'customer_id' : 'nb_new_customer'}, inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8c1aed44-03d3-49f9-b96c-b06a0df03dde",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 36456 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Graphique en nombre de commande\n",
    "purchases_graph = nb_purchases_graph\n",
    "\n",
    "purchases_graph_used = purchases_graph[purchases_graph[\"purchase_date_month\"] >= datetime(2021,3,1)]\n",
    "purchases_graph_used_0 = purchases_graph_used[purchases_graph_used[\"is_customer_known\"]==False]\n",
    "purchases_graph_used_1 = purchases_graph_used[purchases_graph_used[\"is_customer_known\"]==True]\n",
    "\n",
    "\n",
    "# Création du barplot\n",
    "plt.bar(purchases_graph_used_0[\"purchase_date_month\"], purchases_graph_used_0[\"nb_purchases\"], width=12, label = \"Nouveau client\")\n",
    "plt.bar(purchases_graph_used_0[\"purchase_date_month\"], purchases_graph_used_1[\"nb_purchases\"], \n",
    "        bottom = purchases_graph_used_0[\"nb_purchases\"], width=12, label = \"Ancien client\")\n",
    "\n",
    "\n",
    "# commande pr afficher slt\n",
    "plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b%y'))\n",
    "\n",
    "\n",
    "# Ajout de titres et d'étiquettes\n",
    "plt.xlabel('Mois')\n",
    "plt.ylabel(\"Nombre d'achats\")\n",
    "plt.title(\"Nombre d'achats - MUCEM\")\n",
    "plt.legend()\n",
    "\n",
    "# Affichage du barplot\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d312276c-4c46-4d29-b6d6-ed110f59890d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 41620 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# graphique en nombre de client ayant commandé\n",
    "purchases_graph = nb_purchases_graph_2\n",
    "\n",
    "purchases_graph_used = purchases_graph[purchases_graph[\"purchase_date_month\"] >= datetime(2021,4,1)]\n",
    "purchases_graph_used_0 = purchases_graph_used[purchases_graph_used[\"is_customer_known\"]==False]\n",
    "purchases_graph_used_1 = purchases_graph_used[purchases_graph_used[\"is_customer_known\"]==True]\n",
    "\n",
    "\n",
    "# Création du barplot\n",
    "plt.bar(purchases_graph_used_0[\"purchase_date_month\"], purchases_graph_used_0[\"nb_new_customer\"], width=12, label = \"Nouveau client\")\n",
    "plt.bar(purchases_graph_used_0[\"purchase_date_month\"], purchases_graph_used_1[\"nb_new_customer\"], \n",
    "        bottom = purchases_graph_used_0[\"nb_new_customer\"], width=12, label = \"Ancien client\")\n",
    "\n",
    "\n",
    "# commande pr afficher slt\n",
    "plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b%y'))\n",
    "\n",
    "\n",
    "# Ajout de titres et d'étiquettes\n",
    "plt.xlabel('Mois')\n",
    "plt.ylabel(\"Nombre de client ayant commandé\")\n",
    "plt.title(\"Nombre de client ayant commandé un ticket pour l'offre 'muséale groupe'\")\n",
    "plt.legend()\n",
    "\n",
    "# Affichage du barplot\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82895dfc-e5ca-4be0-af24-93c1be8f6248",
   "metadata": {},
   "source": [
    "### Proportion de tickets de prix 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6e27dd83-f188-43a5-b595-618b4922a358",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ticket_id              0.418220\n",
       "customer_id            0.418220\n",
       "purchase_id            0.418220\n",
       "event_type_id          0.418220\n",
       "supplier_name          0.418220\n",
       "purchase_date          0.418220\n",
       "type_of_ticket_name    0.418220\n",
       "amount                 0.418220\n",
       "children               0.418220\n",
       "is_full_price          0.418220\n",
       "name_event_types       0.418220\n",
       "name_facilities        0.418220\n",
       "name_categories        0.402548\n",
       "name_events            0.175585\n",
       "name_seasons           0.418220\n",
       "dtype: float64"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "purchases[purchases['amount'] == 0].count()/len(purchases)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f663d68b-8a5c-4804-b31a-4477a03ca1e4",
   "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>purchase_id</th>\n",
       "      <th>ticket_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>73518.000000</td>\n",
       "      <td>7.351800e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>10.096167</td>\n",
       "      <td>2.484660e+01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>2367.702603</td>\n",
       "      <td>4.636993e+03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>641981.000000</td>\n",
       "      <td>1.256574e+06</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         purchase_id     ticket_id\n",
       "count   73518.000000  7.351800e+04\n",
       "mean       10.096167  2.484660e+01\n",
       "std      2367.702603  4.636993e+03\n",
       "min         1.000000  1.000000e+00\n",
       "25%         1.000000  1.000000e+00\n",
       "50%         1.000000  2.000000e+00\n",
       "75%         1.000000  3.000000e+00\n",
       "max    641981.000000  1.256574e+06"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "purchases.groupby('customer_id')[['purchase_id', 'ticket_id']].nunique().describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d1212b10-3933-450a-b001-9e2cbf308f79",
   "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>type_of_ticket_name</th>\n",
       "      <th>amount</th>\n",
       "      <th>children</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",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>13070859</td>\n",
       "      <td>48187</td>\n",
       "      <td>5107462</td>\n",
       "      <td>4</td>\n",
       "      <td>vente en ligne</td>\n",
       "      <td>2018-12-28 14:47:50+00:00</td>\n",
       "      <td>Atelier</td>\n",
       "      <td>8.0</td>\n",
       "      <td>pricing_formula</td>\n",
       "      <td>False</td>\n",
       "      <td>spectacle vivant</td>\n",
       "      <td>mucem</td>\n",
       "      <td>indiv prog enfant</td>\n",
       "      <td>l'école des magiciens</td>\n",
       "      <td>2018</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>13070860</td>\n",
       "      <td>48187</td>\n",
       "      <td>5107462</td>\n",
       "      <td>4</td>\n",
       "      <td>vente en ligne</td>\n",
       "      <td>2018-12-28 14:47:50+00:00</td>\n",
       "      <td>Atelier</td>\n",
       "      <td>4.0</td>\n",
       "      <td>pricing_formula</td>\n",
       "      <td>False</td>\n",
       "      <td>spectacle vivant</td>\n",
       "      <td>mucem</td>\n",
       "      <td>indiv prog enfant</td>\n",
       "      <td>l'école des magiciens</td>\n",
       "      <td>2018</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>13070861</td>\n",
       "      <td>48187</td>\n",
       "      <td>5107462</td>\n",
       "      <td>4</td>\n",
       "      <td>vente en ligne</td>\n",
       "      <td>2018-12-28 14:47:50+00:00</td>\n",
       "      <td>Atelier</td>\n",
       "      <td>4.0</td>\n",
       "      <td>pricing_formula</td>\n",
       "      <td>False</td>\n",
       "      <td>spectacle vivant</td>\n",
       "      <td>mucem</td>\n",
       "      <td>indiv prog enfant</td>\n",
       "      <td>l'école des magiciens</td>\n",
       "      <td>2018</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>13070862</td>\n",
       "      <td>48187</td>\n",
       "      <td>5107462</td>\n",
       "      <td>4</td>\n",
       "      <td>vente en ligne</td>\n",
       "      <td>2018-12-28 14:47:50+00:00</td>\n",
       "      <td>Atelier</td>\n",
       "      <td>4.0</td>\n",
       "      <td>pricing_formula</td>\n",
       "      <td>False</td>\n",
       "      <td>spectacle vivant</td>\n",
       "      <td>mucem</td>\n",
       "      <td>indiv prog enfant</td>\n",
       "      <td>l'école des magiciens</td>\n",
       "      <td>2018</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>13070863</td>\n",
       "      <td>48187</td>\n",
       "      <td>5107462</td>\n",
       "      <td>4</td>\n",
       "      <td>vente en ligne</td>\n",
       "      <td>2018-12-28 14:47:50+00:00</td>\n",
       "      <td>Atelier</td>\n",
       "      <td>4.0</td>\n",
       "      <td>pricing_formula</td>\n",
       "      <td>False</td>\n",
       "      <td>spectacle vivant</td>\n",
       "      <td>mucem</td>\n",
       "      <td>indiv prog enfant</td>\n",
       "      <td>l'école des magiciens</td>\n",
       "      <td>2018</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",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1826667</th>\n",
       "      <td>20662815</td>\n",
       "      <td>1256135</td>\n",
       "      <td>8007697</td>\n",
       "      <td>5</td>\n",
       "      <td>vente en ligne</td>\n",
       "      <td>2023-11-08 17:23:54+00:00</td>\n",
       "      <td>Atelier</td>\n",
       "      <td>11.0</td>\n",
       "      <td>pricing_formula</td>\n",
       "      <td>False</td>\n",
       "      <td>offre muséale groupe</td>\n",
       "      <td>mucem</td>\n",
       "      <td>indiv entrées tp</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2023</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1826668</th>\n",
       "      <td>20662816</td>\n",
       "      <td>1256136</td>\n",
       "      <td>8007698</td>\n",
       "      <td>5</td>\n",
       "      <td>vente en ligne</td>\n",
       "      <td>2023-11-08 18:32:18+00:00</td>\n",
       "      <td>Atelier</td>\n",
       "      <td>11.0</td>\n",
       "      <td>pricing_formula</td>\n",
       "      <td>False</td>\n",
       "      <td>offre muséale groupe</td>\n",
       "      <td>mucem</td>\n",
       "      <td>indiv entrées tp</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2023</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1826669</th>\n",
       "      <td>20662817</td>\n",
       "      <td>1256136</td>\n",
       "      <td>8007698</td>\n",
       "      <td>5</td>\n",
       "      <td>vente en ligne</td>\n",
       "      <td>2023-11-08 18:32:18+00:00</td>\n",
       "      <td>Atelier</td>\n",
       "      <td>11.0</td>\n",
       "      <td>pricing_formula</td>\n",
       "      <td>False</td>\n",
       "      <td>offre muséale groupe</td>\n",
       "      <td>mucem</td>\n",
       "      <td>indiv entrées tp</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2023</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1826670</th>\n",
       "      <td>20662818</td>\n",
       "      <td>1256137</td>\n",
       "      <td>8007699</td>\n",
       "      <td>5</td>\n",
       "      <td>vente en ligne</td>\n",
       "      <td>2023-11-08 19:30:28+00:00</td>\n",
       "      <td>Atelier</td>\n",
       "      <td>11.0</td>\n",
       "      <td>pricing_formula</td>\n",
       "      <td>False</td>\n",
       "      <td>offre muséale groupe</td>\n",
       "      <td>mucem</td>\n",
       "      <td>indiv entrées tp</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2023</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1826671</th>\n",
       "      <td>20662819</td>\n",
       "      <td>1256137</td>\n",
       "      <td>8007699</td>\n",
       "      <td>5</td>\n",
       "      <td>vente en ligne</td>\n",
       "      <td>2023-11-08 19:30:28+00:00</td>\n",
       "      <td>Atelier</td>\n",
       "      <td>11.0</td>\n",
       "      <td>pricing_formula</td>\n",
       "      <td>False</td>\n",
       "      <td>offre muséale groupe</td>\n",
       "      <td>mucem</td>\n",
       "      <td>indiv entrées tp</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2023</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1826672 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         ticket_id  customer_id  purchase_id  event_type_id   supplier_name  \\\n",
       "0         13070859        48187      5107462              4  vente en ligne   \n",
       "1         13070860        48187      5107462              4  vente en ligne   \n",
       "2         13070861        48187      5107462              4  vente en ligne   \n",
       "3         13070862        48187      5107462              4  vente en ligne   \n",
       "4         13070863        48187      5107462              4  vente en ligne   \n",
       "...            ...          ...          ...            ...             ...   \n",
       "1826667   20662815      1256135      8007697              5  vente en ligne   \n",
       "1826668   20662816      1256136      8007698              5  vente en ligne   \n",
       "1826669   20662817      1256136      8007698              5  vente en ligne   \n",
       "1826670   20662818      1256137      8007699              5  vente en ligne   \n",
       "1826671   20662819      1256137      8007699              5  vente en ligne   \n",
       "\n",
       "                    purchase_date type_of_ticket_name  amount  \\\n",
       "0       2018-12-28 14:47:50+00:00             Atelier     8.0   \n",
       "1       2018-12-28 14:47:50+00:00             Atelier     4.0   \n",
       "2       2018-12-28 14:47:50+00:00             Atelier     4.0   \n",
       "3       2018-12-28 14:47:50+00:00             Atelier     4.0   \n",
       "4       2018-12-28 14:47:50+00:00             Atelier     4.0   \n",
       "...                           ...                 ...     ...   \n",
       "1826667 2023-11-08 17:23:54+00:00             Atelier    11.0   \n",
       "1826668 2023-11-08 18:32:18+00:00             Atelier    11.0   \n",
       "1826669 2023-11-08 18:32:18+00:00             Atelier    11.0   \n",
       "1826670 2023-11-08 19:30:28+00:00             Atelier    11.0   \n",
       "1826671 2023-11-08 19:30:28+00:00             Atelier    11.0   \n",
       "\n",
       "                children  is_full_price      name_event_types name_facilities  \\\n",
       "0        pricing_formula          False      spectacle vivant           mucem   \n",
       "1        pricing_formula          False      spectacle vivant           mucem   \n",
       "2        pricing_formula          False      spectacle vivant           mucem   \n",
       "3        pricing_formula          False      spectacle vivant           mucem   \n",
       "4        pricing_formula          False      spectacle vivant           mucem   \n",
       "...                  ...            ...                   ...             ...   \n",
       "1826667  pricing_formula          False  offre muséale groupe           mucem   \n",
       "1826668  pricing_formula          False  offre muséale groupe           mucem   \n",
       "1826669  pricing_formula          False  offre muséale groupe           mucem   \n",
       "1826670  pricing_formula          False  offre muséale groupe           mucem   \n",
       "1826671  pricing_formula          False  offre muséale groupe           mucem   \n",
       "\n",
       "           name_categories            name_events  name_seasons  \n",
       "0        indiv prog enfant  l'école des magiciens          2018  \n",
       "1        indiv prog enfant  l'école des magiciens          2018  \n",
       "2        indiv prog enfant  l'école des magiciens          2018  \n",
       "3        indiv prog enfant  l'école des magiciens          2018  \n",
       "4        indiv prog enfant  l'école des magiciens          2018  \n",
       "...                    ...                    ...           ...  \n",
       "1826667   indiv entrées tp                    NaN          2023  \n",
       "1826668   indiv entrées tp                    NaN          2023  \n",
       "1826669   indiv entrées tp                    NaN          2023  \n",
       "1826670   indiv entrées tp                    NaN          2023  \n",
       "1826671   indiv entrées tp                    NaN          2023  \n",
       "\n",
       "[1826672 rows x 15 columns]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "purchases"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8a90eaa-c383-4f73-9fd6-6fbbe8eeefb8",
   "metadata": {},
   "source": [
    "# 2 - Comportement d'achat bis (Alexis)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "dc45c1cd-2a78-48a6-aa2b-6a501254b6f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(156289, 40)\n"
     ]
    },
    {
     "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>birthdate</th>\n",
       "      <th>street_id</th>\n",
       "      <th>is_partner</th>\n",
       "      <th>gender</th>\n",
       "      <th>is_email_true</th>\n",
       "      <th>opt_in</th>\n",
       "      <th>structure_id</th>\n",
       "      <th>profession</th>\n",
       "      <th>language</th>\n",
       "      <th>...</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>name_event_types</th>\n",
       "      <th>avg_amount</th>\n",
       "      <th>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "      <th>time_to_open</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>False</td>\n",
       "      <td>2</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3262.190868</td>\n",
       "      <td>4.179306</td>\n",
       "      <td>3258.011562</td>\n",
       "      <td>51.0</td>\n",
       "      <td>offre muséale individuel</td>\n",
       "      <td>6.150659</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>False</td>\n",
       "      <td>2</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2502.715509</td>\n",
       "      <td>1408.715532</td>\n",
       "      <td>1093.999977</td>\n",
       "      <td>5.0</td>\n",
       "      <td>formule adhésion</td>\n",
       "      <td>6.439463</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>False</td>\n",
       "      <td>2</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3698.198229</td>\n",
       "      <td>5.221840</td>\n",
       "      <td>3692.976389</td>\n",
       "      <td>2988.0</td>\n",
       "      <td>spectacle vivant</td>\n",
       "      <td>7.762474</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>False</td>\n",
       "      <td>2</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3803.369792</td>\n",
       "      <td>0.146331</td>\n",
       "      <td>3803.223461</td>\n",
       "      <td>9.0</td>\n",
       "      <td>offre muséale groupe</td>\n",
       "      <td>4.452618</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>False</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1705.261192</td>\n",
       "      <td>1456.333715</td>\n",
       "      <td>248.927477</td>\n",
       "      <td>0.0</td>\n",
       "      <td>formule adhésion</td>\n",
       "      <td>6.439463</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 40 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   customer_id birthdate  street_id  is_partner  gender  is_email_true  \\\n",
       "0            1       NaN          2       False       2           True   \n",
       "1            1       NaN          2       False       2           True   \n",
       "2            1       NaN          2       False       2           True   \n",
       "3            1       NaN          2       False       2           True   \n",
       "4            2       NaN          2       False       1           True   \n",
       "\n",
       "   opt_in  structure_id profession language  ...  vente_internet_max  \\\n",
       "0   False           NaN        NaN      NaN  ...                 1.0   \n",
       "1   False           NaN        NaN      NaN  ...                 1.0   \n",
       "2   False           NaN        NaN      NaN  ...                 1.0   \n",
       "3   False           NaN        NaN      NaN  ...                 1.0   \n",
       "4    True           NaN        NaN      NaN  ...                 0.0   \n",
       "\n",
       "  purchase_date_min  purchase_date_max  time_between_purchase  \\\n",
       "0       3262.190868           4.179306            3258.011562   \n",
       "1       2502.715509        1408.715532            1093.999977   \n",
       "2       3698.198229           5.221840            3692.976389   \n",
       "3       3803.369792           0.146331            3803.223461   \n",
       "4       1705.261192        1456.333715             248.927477   \n",
       "\n",
       "   nb_tickets_internet          name_event_types  avg_amount  nb_campaigns  \\\n",
       "0                 51.0  offre muséale individuel    6.150659           NaN   \n",
       "1                  5.0          formule adhésion    6.439463           NaN   \n",
       "2               2988.0          spectacle vivant    7.762474           NaN   \n",
       "3                  9.0      offre muséale groupe    4.452618           NaN   \n",
       "4                  0.0          formule adhésion    6.439463           4.0   \n",
       "\n",
       "   nb_campaigns_opened  time_to_open  \n",
       "0                  NaN           NaN  \n",
       "1                  NaN           NaN  \n",
       "2                  NaN           NaN  \n",
       "3                  NaN           NaN  \n",
       "4                  NaN           NaN  \n",
       "\n",
       "[5 rows x 40 columns]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Chargement des données temporaires\n",
    "BUCKET = \"projet-bdc2324-team1\"\n",
    "FILE_KEY_S3 = \"0_Temp/Company 1 - customer_event.csv\"\n",
    "FILE_PATH_S3 = BUCKET + \"/\" + FILE_KEY_S3\n",
    "\n",
    "with fs.open(FILE_PATH_S3, mode=\"rb\") as file_in:\n",
    "    customer = pd.read_csv(file_in, sep=\",\")\n",
    "\n",
    "print(customer.shape)\n",
    "customer.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "89fcb455-efb4-4ad4-ab88-efd6c8a76287",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['customer_id', 'birthdate', 'street_id', 'is_partner', 'gender',\n",
       "       'is_email_true', 'opt_in', 'structure_id', 'profession', 'language',\n",
       "       'mcp_contact_id', 'last_buying_date', 'max_price', 'ticket_sum',\n",
       "       'average_price', 'fidelity', 'average_purchase_delay',\n",
       "       'average_price_basket', 'average_ticket_basket', 'total_price',\n",
       "       'purchase_count', 'first_buying_date', 'country', 'age', 'tenant_id',\n",
       "       'event_type_id', 'nb_tickets', 'nb_purchases', 'total_amount',\n",
       "       'nb_suppliers', 'vente_internet_max', 'purchase_date_min',\n",
       "       'purchase_date_max', 'time_between_purchase', 'nb_tickets_internet',\n",
       "       'name_event_types', 'avg_amount', 'nb_campaigns', 'nb_campaigns_opened',\n",
       "       'time_to_open'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customer.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d7b2356a-d5fc-4547-b3ff-fded0e304fb6",
   "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>average_price</th>\n",
       "      <th>average_purchase_delay</th>\n",
       "      <th>average_price_basket</th>\n",
       "      <th>average_ticket_basket</th>\n",
       "      <th>purchase_count</th>\n",
       "      <th>total_price</th>\n",
       "      <th>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>7.030122</td>\n",
       "      <td>-67.790969</td>\n",
       "      <td>13.751530</td>\n",
       "      <td>1.956087</td>\n",
       "      <td>641472</td>\n",
       "      <td>8821221.5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>307</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>3</td>\n",
       "      <td>18.333333</td>\n",
       "      <td>30.666667</td>\n",
       "      <td>36.666667</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3</td>\n",
       "      <td>110.0</td>\n",
       "      <td>222.0</td>\n",
       "      <td>124.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>4</td>\n",
       "      <td>10.250000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>20.500000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>2</td>\n",
       "      <td>41.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>5</td>\n",
       "      <td>9.500000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>19.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>1</td>\n",
       "      <td>19.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   customer_id  average_price  average_purchase_delay  average_price_basket  \\\n",
       "0            1       7.030122              -67.790969             13.751530   \n",
       "4            2       0.000000                0.000000              0.000000   \n",
       "6            3      18.333333               30.666667             36.666667   \n",
       "7            4      10.250000                5.000000             20.500000   \n",
       "9            5       9.500000                0.000000             19.000000   \n",
       "\n",
       "   average_ticket_basket  purchase_count  total_price  nb_campaigns  \\\n",
       "0               1.956087          641472    8821221.5           0.0   \n",
       "4               1.000000             307          0.0           4.0   \n",
       "6               2.000000               3        110.0         222.0   \n",
       "7               2.000000               2         41.0           7.0   \n",
       "9               2.000000               1         19.0           4.0   \n",
       "\n",
       "   nb_campaigns_opened  \n",
       "0                  0.0  \n",
       "4                  0.0  \n",
       "6                124.0  \n",
       "7                  7.0  \n",
       "9                  0.0  "
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "achat = ['customer_id', 'average_price', 'average_purchase_delay', 'average_price_basket',\n",
    "          'average_ticket_basket', 'purchase_count', 'total_price', 'nb_campaigns',\n",
    "         'nb_campaigns_opened']\n",
    "\n",
    "customer_achat = customer[achat].drop_duplicates(subset = ['customer_id'])\n",
    "customer_achat['nb_campaigns'] = customer_achat['nb_campaigns'].fillna(0)\n",
    "customer_achat['nb_campaigns_opened'] = customer_achat['nb_campaigns_opened'].fillna(0)\n",
    "customer_achat = customer_achat.fillna(0)\n",
    "customer_achat.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5559748f-1745-4651-a9f6-94702c7ee66f",
   "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>average_price</th>\n",
       "      <th>average_purchase_delay</th>\n",
       "      <th>average_price_basket</th>\n",
       "      <th>average_ticket_basket</th>\n",
       "      <th>purchase_count</th>\n",
       "      <th>total_price</th>\n",
       "      <th>nb_campaigns</th>\n",
       "      <th>nb_campaigns_opened</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>151865.000000</td>\n",
       "      <td>151865.000000</td>\n",
       "      <td>151865.000000</td>\n",
       "      <td>151865.000000</td>\n",
       "      <td>151865.000000</td>\n",
       "      <td>151865.000000</td>\n",
       "      <td>151865.000000</td>\n",
       "      <td>151865.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>5.252070</td>\n",
       "      <td>-206.581486</td>\n",
       "      <td>11.451596</td>\n",
       "      <td>1.723372</td>\n",
       "      <td>0.655148</td>\n",
       "      <td>16.994064</td>\n",
       "      <td>40.923241</td>\n",
       "      <td>7.870681</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>7.915955</td>\n",
       "      <td>2996.743657</td>\n",
       "      <td>48.271194</td>\n",
       "      <td>7.045950</td>\n",
       "      <td>5.694038</td>\n",
       "      <td>313.099102</td>\n",
       "      <td>70.445724</td>\n",
       "      <td>23.119061</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>-44863.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>11.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>19.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>32.000000</td>\n",
       "      <td>3.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>320.000000</td>\n",
       "      <td>1914.000000</td>\n",
       "      <td>9900.000000</td>\n",
       "      <td>900.000000</td>\n",
       "      <td>1508.000000</td>\n",
       "      <td>64350.000000</td>\n",
       "      <td>439.000000</td>\n",
       "      <td>434.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       average_price  average_purchase_delay  average_price_basket  \\\n",
       "count  151865.000000           151865.000000         151865.000000   \n",
       "mean        5.252070             -206.581486             11.451596   \n",
       "std         7.915955             2996.743657             48.271194   \n",
       "min         0.000000           -44863.000000              0.000000   \n",
       "25%         0.000000                0.000000              0.000000   \n",
       "50%         0.000000                0.000000              0.000000   \n",
       "75%        11.000000                0.000000             19.000000   \n",
       "max       320.000000             1914.000000           9900.000000   \n",
       "\n",
       "       average_ticket_basket  purchase_count    total_price   nb_campaigns  \\\n",
       "count          151865.000000   151865.000000  151865.000000  151865.000000   \n",
       "mean                1.723372        0.655148      16.994064      40.923241   \n",
       "std                 7.045950        5.694038     313.099102      70.445724   \n",
       "min                 0.000000        0.000000       0.000000       0.000000   \n",
       "25%                 0.000000        0.000000       0.000000       2.000000   \n",
       "50%                 0.000000        0.000000       0.000000       5.000000   \n",
       "75%                 2.000000        1.000000      20.000000      32.000000   \n",
       "max               900.000000     1508.000000   64350.000000     439.000000   \n",
       "\n",
       "       nb_campaigns_opened  \n",
       "count        151865.000000  \n",
       "mean              7.870681  \n",
       "std              23.119061  \n",
       "min               0.000000  \n",
       "25%               0.000000  \n",
       "50%               1.000000  \n",
       "75%               3.000000  \n",
       "max             434.000000  "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customer_wto_outlier = customer_achat[customer_achat['customer_id']!=1]\n",
    "\n",
    "customer_wto_outlier[['average_price', 'average_purchase_delay', 'average_price_basket',\n",
    "          'average_ticket_basket', 'purchase_count', 'total_price', 'nb_campaigns', 'nb_campaigns_opened']].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b49c9e93-f324-42ee-a262-34ffb44a2261",
   "metadata": {},
   "source": [
    "# Event"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "4971e35d-a762-4e18-9443-fd9571bd3f1e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 49912 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Nombre de consommateurs uniques par type d'évènement\n",
    "\n",
    "event_counts = customer.groupby('name_event_types')['customer_id'].nunique()\n",
    "\n",
    "event_counts.plot(kind='bar')\n",
    "plt.xlabel(\"Type d'évènement\")\n",
    "plt.ylabel('Nombre de consommateurs uniques')\n",
    "plt.title(\"Nombre de consommateurs uniques par type d'évènement\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "bc65a711-d172-4839-b487-3047280fc3a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 58464 characters]",
      "text/plain": [
       "<Figure size 800x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Nombre Total de tickets achetés par Type d'évènements\n",
    "\n",
    "total_tickets_by_event = customer.groupby('name_event_types')['nb_tickets'].sum()\n",
    "\n",
    "total_tickets_by_event.plot(kind='bar', figsize=(8, 5))\n",
    "plt.xlabel(\"Type d'évènements\")\n",
    "plt.ylabel('Nombre Total de tickets achetés')\n",
    "plt.title(\"Nombre Total de tickets achetés par Type d'évènements\")\n",
    "plt.xticks(rotation=45)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c95cc35c-abfc-47c7-9b8a-ac69bfd60dd8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 48580 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Nombre de Canaux de Ventes Moyen utilisé par les Consommateurs par type d'évènement\n",
    "\n",
    "avg_supp_event = customer.groupby('name_event_types')['nb_suppliers'].mean()\n",
    "avg_supp_event.plot(kind='bar')\n",
    "plt.xlabel(\"Type d'évènement\")\n",
    "plt.ylabel('Nombre de Canaux de Ventes Moyen')\n",
    "plt.title(\"Nombre de Canaux de Ventes Moyen utilisé par les Consommateurs par type d'évènement\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "49d5fd2d-9bc1-43ac-9270-1efd73759854",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 61120 characters]",
      "text/plain": [
       "<Figure size 800x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Nombre Total de tickets achetés sur Internet par Type d'évènements\n",
    "\n",
    "nb_tickets_internet = customer.groupby('name_event_types')['nb_tickets_internet'].sum()\n",
    "nb_tickets_internet.plot(kind='bar', figsize=(8, 5))\n",
    "plt.xlabel(\"Type d'évènements\")\n",
    "plt.ylabel('Nombre Total de tickets achetés sur Internet')\n",
    "plt.title(\"Nombre Total de tickets achetés sur Internet par Type d'évènements\")\n",
    "plt.xticks(rotation=45)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc071992-cf4d-4b9f-9c3b-3f0e98e20eff",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "4f9561a9-6a94-434e-b8e7-9b708f5b5529",
   "metadata": {},
   "source": [
    "# 3 - Caractéristiques Démographiques (peu exploitable)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e50e2583-4b8f-478e-87ac-591dde200af8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['customer_id', 'birthdate', 'street_id', 'is_partner', 'gender',\n",
       "       'is_email_true', 'opt_in', 'structure_id', 'profession', 'language',\n",
       "       'mcp_contact_id', 'last_buying_date', 'max_price', 'ticket_sum',\n",
       "       'average_price', 'fidelity', 'average_purchase_delay',\n",
       "       'average_price_basket', 'average_ticket_basket', 'total_price',\n",
       "       'purchase_count', 'first_buying_date', 'country', 'age', 'tenant_id',\n",
       "       'event_type_id', 'nb_tickets', 'nb_purchases', 'total_amount',\n",
       "       'nb_suppliers', 'vente_internet_max', 'purchase_date_min',\n",
       "       'purchase_date_max', 'time_between_purchase', 'nb_tickets_internet',\n",
       "       'name_event_types', 'avg_amount', 'nb_campaigns', 'nb_campaigns_opened',\n",
       "       'time_to_open'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customer.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c724a315-9fe8-4874-be8f-a8115b17b5e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def percent_of_na(df, column):\n",
    "    na_percentage = df[column].isna().mean() * 100\n",
    "    non_na_percentage = 100 - na_percentage\n",
    "    \n",
    "    labels = ['Valeurs Manquantes', 'Non-Valeurs Manquantes']\n",
    "    sizes = [na_percentage, non_na_percentage]\n",
    "    colors = ['#ff9999','#66b3ff']\n",
    "    explode = (0.1, 0)\n",
    "\n",
    "    plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', startangle=140)\n",
    "    plt.axis('equal') \n",
    "    plt.title('Pourcentage de Valeurs Manquantes : {}'.format(column))\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "58af5dcb-673e-4f4d-ad5c-f66ce1e8a22c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32376 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "percent_of_na(customer, 'profession')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "cc3437f7-8b36-4398-9da6-ff15e8e4c8d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 31516 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "percent_of_na(customer, 'language')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c34164d2-5ab2-4923-a165-30dc5c070233",
   "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
}
