From e8c2e785f2437c642a9210da26becc621221f979 Mon Sep 17 00:00:00 2001
From: Camilla Scandola <103769428+camilla-scandola@users.noreply.github.com>
Date: Wed, 22 Oct 2025 22:57:11 +0200
Subject: [PATCH] Solved Lab
---
lab-dw-aggregating.ipynb | 1175 +++++++++++++++++++++++++++++++++-----
1 file changed, 1035 insertions(+), 140 deletions(-)
diff --git a/lab-dw-aggregating.ipynb b/lab-dw-aggregating.ipynb
index fadd718..b3ff8d8 100644
--- a/lab-dw-aggregating.ipynb
+++ b/lab-dw-aggregating.ipynb
@@ -1,165 +1,1060 @@
{
- "cells": [
- {
- "cell_type": "markdown",
- "id": "31969215-2a90-4d8b-ac36-646a7ae13744",
- "metadata": {
- "id": "31969215-2a90-4d8b-ac36-646a7ae13744"
- },
- "source": [
- "# Lab | Data Aggregation and Filtering"
- ]
- },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "31969215-2a90-4d8b-ac36-646a7ae13744",
+ "metadata": {
+ "id": "31969215-2a90-4d8b-ac36-646a7ae13744"
+ },
+ "source": [
+ "# Lab | Data Aggregation and Filtering"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "a8f08a52-bec0-439b-99cc-11d3809d8b5d",
+ "metadata": {
+ "id": "a8f08a52-bec0-439b-99cc-11d3809d8b5d"
+ },
+ "source": [
+ "In this challenge, we will continue to work with customer data from an insurance company. We will use the dataset called marketing_customer_analysis.csv, which can be found at the following link:\n",
+ "\n",
+ "https://raw.githubusercontent.com/data-bootcamp-v4/data/main/marketing_customer_analysis.csv\n",
+ "\n",
+ "This dataset contains information such as customer demographics, policy details, vehicle information, and the customer's response to the last marketing campaign. Our goal is to explore and analyze this data by first performing data cleaning, formatting, and structuring."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "9c98ddc5-b041-4c94-ada1-4dfee5c98e50",
+ "metadata": {
+ "id": "9c98ddc5-b041-4c94-ada1-4dfee5c98e50"
+ },
+ "source": [
+ "1. Create a new DataFrame that only includes customers who:\n",
+ " - have a **low total_claim_amount** (e.g., below $1,000),\n",
+ " - have a response \"Yes\" to the last marketing campaign."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "b9be383e-5165-436e-80c8-57d4c757c8c3",
+ "metadata": {
+ "id": "b9be383e-5165-436e-80c8-57d4c757c8c3"
+ },
+ "source": [
+ "2. Using the original Dataframe, analyze:\n",
+ " - the average `monthly_premium` and/or customer lifetime value by `policy_type` and `gender` for customers who responded \"Yes\", and\n",
+ " - compare these insights to `total_claim_amount` patterns, and discuss which segments appear most profitable or low-risk for the company."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "7050f4ac-53c5-4193-a3c0-8699b87196f0",
+ "metadata": {
+ "id": "7050f4ac-53c5-4193-a3c0-8699b87196f0"
+ },
+ "source": [
+ "3. Analyze the total number of customers who have policies in each state, and then filter the results to only include states where there are more than 500 customers."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "b60a4443-a1a7-4bbf-b78e-9ccdf9895e0d",
+ "metadata": {
+ "id": "b60a4443-a1a7-4bbf-b78e-9ccdf9895e0d"
+ },
+ "source": [
+ "4. Find the maximum, minimum, and median customer lifetime value by education level and gender. Write your conclusions."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "id": "28dbe01e-dbd9-495e-b6ff-2f02fee72e0a",
+ "metadata": {
+ "scrolled": true
+ },
+ "outputs": [
{
- "cell_type": "markdown",
- "id": "a8f08a52-bec0-439b-99cc-11d3809d8b5d",
- "metadata": {
- "id": "a8f08a52-bec0-439b-99cc-11d3809d8b5d"
- },
- "source": [
- "In this challenge, we will continue to work with customer data from an insurance company. We will use the dataset called marketing_customer_analysis.csv, which can be found at the following link:\n",
- "\n",
- "https://raw.githubusercontent.com/data-bootcamp-v4/data/main/marketing_customer_analysis.csv\n",
- "\n",
- "This dataset contains information such as customer demographics, policy details, vehicle information, and the customer's response to the last marketing campaign. Our goal is to explore and analyze this data by first performing data cleaning, formatting, and structuring."
+ "data": {
+ "text/html": [
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " Unnamed: 0 | \n",
+ " Customer | \n",
+ " State | \n",
+ " Customer Lifetime Value | \n",
+ " Response | \n",
+ " Coverage | \n",
+ " Education | \n",
+ " Effective To Date | \n",
+ " EmploymentStatus | \n",
+ " Gender | \n",
+ " ... | \n",
+ " Number of Open Complaints | \n",
+ " Number of Policies | \n",
+ " Policy Type | \n",
+ " Policy | \n",
+ " Renew Offer Type | \n",
+ " Sales Channel | \n",
+ " Total Claim Amount | \n",
+ " Vehicle Class | \n",
+ " Vehicle Size | \n",
+ " Vehicle Type | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " 0 | \n",
+ " DK49336 | \n",
+ " Arizona | \n",
+ " 4809.216960 | \n",
+ " No | \n",
+ " Basic | \n",
+ " College | \n",
+ " 2/18/11 | \n",
+ " Employed | \n",
+ " M | \n",
+ " ... | \n",
+ " 0.0 | \n",
+ " 9 | \n",
+ " Corporate Auto | \n",
+ " Corporate L3 | \n",
+ " Offer3 | \n",
+ " Agent | \n",
+ " 292.800000 | \n",
+ " Four-Door Car | \n",
+ " Medsize | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ " | 1 | \n",
+ " 1 | \n",
+ " KX64629 | \n",
+ " California | \n",
+ " 2228.525238 | \n",
+ " No | \n",
+ " Basic | \n",
+ " College | \n",
+ " 1/18/11 | \n",
+ " Unemployed | \n",
+ " F | \n",
+ " ... | \n",
+ " 0.0 | \n",
+ " 1 | \n",
+ " Personal Auto | \n",
+ " Personal L3 | \n",
+ " Offer4 | \n",
+ " Call Center | \n",
+ " 744.924331 | \n",
+ " Four-Door Car | \n",
+ " Medsize | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " 2 | \n",
+ " LZ68649 | \n",
+ " Washington | \n",
+ " 14947.917300 | \n",
+ " No | \n",
+ " Basic | \n",
+ " Bachelor | \n",
+ " 2/10/11 | \n",
+ " Employed | \n",
+ " M | \n",
+ " ... | \n",
+ " 0.0 | \n",
+ " 2 | \n",
+ " Personal Auto | \n",
+ " Personal L3 | \n",
+ " Offer3 | \n",
+ " Call Center | \n",
+ " 480.000000 | \n",
+ " SUV | \n",
+ " Medsize | \n",
+ " A | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " 3 | \n",
+ " XL78013 | \n",
+ " Oregon | \n",
+ " 22332.439460 | \n",
+ " Yes | \n",
+ " Extended | \n",
+ " College | \n",
+ " 1/11/11 | \n",
+ " Employed | \n",
+ " M | \n",
+ " ... | \n",
+ " 0.0 | \n",
+ " 2 | \n",
+ " Corporate Auto | \n",
+ " Corporate L3 | \n",
+ " Offer2 | \n",
+ " Branch | \n",
+ " 484.013411 | \n",
+ " Four-Door Car | \n",
+ " Medsize | \n",
+ " A | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " 4 | \n",
+ " QA50777 | \n",
+ " Oregon | \n",
+ " 9025.067525 | \n",
+ " No | \n",
+ " Premium | \n",
+ " Bachelor | \n",
+ " 1/17/11 | \n",
+ " Medical Leave | \n",
+ " F | \n",
+ " ... | \n",
+ " NaN | \n",
+ " 7 | \n",
+ " Personal Auto | \n",
+ " Personal L2 | \n",
+ " Offer1 | \n",
+ " Branch | \n",
+ " 707.925645 | \n",
+ " Four-Door Car | \n",
+ " Medsize | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
5 rows × 26 columns
\n",
+ "
"
+ ],
+ "text/plain": [
+ " Unnamed: 0 Customer State Customer Lifetime Value Response \\\n",
+ "0 0 DK49336 Arizona 4809.216960 No \n",
+ "1 1 KX64629 California 2228.525238 No \n",
+ "2 2 LZ68649 Washington 14947.917300 No \n",
+ "3 3 XL78013 Oregon 22332.439460 Yes \n",
+ "4 4 QA50777 Oregon 9025.067525 No \n",
+ "\n",
+ " Coverage Education Effective To Date EmploymentStatus Gender ... \\\n",
+ "0 Basic College 2/18/11 Employed M ... \n",
+ "1 Basic College 1/18/11 Unemployed F ... \n",
+ "2 Basic Bachelor 2/10/11 Employed M ... \n",
+ "3 Extended College 1/11/11 Employed M ... \n",
+ "4 Premium Bachelor 1/17/11 Medical Leave F ... \n",
+ "\n",
+ " Number of Open Complaints Number of Policies Policy Type Policy \\\n",
+ "0 0.0 9 Corporate Auto Corporate L3 \n",
+ "1 0.0 1 Personal Auto Personal L3 \n",
+ "2 0.0 2 Personal Auto Personal L3 \n",
+ "3 0.0 2 Corporate Auto Corporate L3 \n",
+ "4 NaN 7 Personal Auto Personal L2 \n",
+ "\n",
+ " Renew Offer Type Sales Channel Total Claim Amount Vehicle Class \\\n",
+ "0 Offer3 Agent 292.800000 Four-Door Car \n",
+ "1 Offer4 Call Center 744.924331 Four-Door Car \n",
+ "2 Offer3 Call Center 480.000000 SUV \n",
+ "3 Offer2 Branch 484.013411 Four-Door Car \n",
+ "4 Offer1 Branch 707.925645 Four-Door Car \n",
+ "\n",
+ " Vehicle Size Vehicle Type \n",
+ "0 Medsize NaN \n",
+ "1 Medsize NaN \n",
+ "2 Medsize A \n",
+ "3 Medsize A \n",
+ "4 Medsize NaN \n",
+ "\n",
+ "[5 rows x 26 columns]"
]
- },
+ },
+ "execution_count": 1,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "import pandas as pd\n",
+ "\n",
+ "url = 'https://raw.githubusercontent.com/data-bootcamp-v4/data/main/marketing_customer_analysis.csv'\n",
+ "df = pd.read_csv(url)\n",
+ "\n",
+ "df.head()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "id": "c96b5d3f-b7fd-4363-8290-73d75c1f0901",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "df = pd.read_csv(url)\n",
+ "# standardize column names\n",
+ "df.columns = df.columns.str.strip().str.lower().str.replace(\" \", \"_\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "9ed1c9f1-28ba-4b41-97ab-05c72f576c61",
+ "metadata": {},
+ "source": [
+ "1. Dataframe filtered by low claim amount and responded yes:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "id": "c7920243-0303-4c9e-a1d0-da70e8f05e72",
+ "metadata": {
+ "scrolled": true
+ },
+ "outputs": [
{
- "cell_type": "markdown",
- "id": "9c98ddc5-b041-4c94-ada1-4dfee5c98e50",
- "metadata": {
- "id": "9c98ddc5-b041-4c94-ada1-4dfee5c98e50"
- },
- "source": [
- "1. Create a new DataFrame that only includes customers who:\n",
- " - have a **low total_claim_amount** (e.g., below $1,000),\n",
- " - have a response \"Yes\" to the last marketing campaign."
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " unnamed:_0 | \n",
+ " customer | \n",
+ " state | \n",
+ " customer_lifetime_value | \n",
+ " response | \n",
+ " coverage | \n",
+ " education | \n",
+ " effective_to_date | \n",
+ " employmentstatus | \n",
+ " gender | \n",
+ " ... | \n",
+ " number_of_open_complaints | \n",
+ " number_of_policies | \n",
+ " policy_type | \n",
+ " policy | \n",
+ " renew_offer_type | \n",
+ " sales_channel | \n",
+ " total_claim_amount | \n",
+ " vehicle_class | \n",
+ " vehicle_size | \n",
+ " vehicle_type | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " 0 | \n",
+ " DK49336 | \n",
+ " Arizona | \n",
+ " 4809.216960 | \n",
+ " No | \n",
+ " Basic | \n",
+ " College | \n",
+ " 2/18/11 | \n",
+ " Employed | \n",
+ " M | \n",
+ " ... | \n",
+ " 0.0 | \n",
+ " 9 | \n",
+ " Corporate Auto | \n",
+ " Corporate L3 | \n",
+ " Offer3 | \n",
+ " Agent | \n",
+ " 292.800000 | \n",
+ " Four-Door Car | \n",
+ " Medsize | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ " | 1 | \n",
+ " 1 | \n",
+ " KX64629 | \n",
+ " California | \n",
+ " 2228.525238 | \n",
+ " No | \n",
+ " Basic | \n",
+ " College | \n",
+ " 1/18/11 | \n",
+ " Unemployed | \n",
+ " F | \n",
+ " ... | \n",
+ " 0.0 | \n",
+ " 1 | \n",
+ " Personal Auto | \n",
+ " Personal L3 | \n",
+ " Offer4 | \n",
+ " Call Center | \n",
+ " 744.924331 | \n",
+ " Four-Door Car | \n",
+ " Medsize | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " 2 | \n",
+ " LZ68649 | \n",
+ " Washington | \n",
+ " 14947.917300 | \n",
+ " No | \n",
+ " Basic | \n",
+ " Bachelor | \n",
+ " 2/10/11 | \n",
+ " Employed | \n",
+ " M | \n",
+ " ... | \n",
+ " 0.0 | \n",
+ " 2 | \n",
+ " Personal Auto | \n",
+ " Personal L3 | \n",
+ " Offer3 | \n",
+ " Call Center | \n",
+ " 480.000000 | \n",
+ " SUV | \n",
+ " Medsize | \n",
+ " A | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " 3 | \n",
+ " XL78013 | \n",
+ " Oregon | \n",
+ " 22332.439460 | \n",
+ " Yes | \n",
+ " Extended | \n",
+ " College | \n",
+ " 1/11/11 | \n",
+ " Employed | \n",
+ " M | \n",
+ " ... | \n",
+ " 0.0 | \n",
+ " 2 | \n",
+ " Corporate Auto | \n",
+ " Corporate L3 | \n",
+ " Offer2 | \n",
+ " Branch | \n",
+ " 484.013411 | \n",
+ " Four-Door Car | \n",
+ " Medsize | \n",
+ " A | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " 4 | \n",
+ " QA50777 | \n",
+ " Oregon | \n",
+ " 9025.067525 | \n",
+ " No | \n",
+ " Premium | \n",
+ " Bachelor | \n",
+ " 1/17/11 | \n",
+ " Medical Leave | \n",
+ " F | \n",
+ " ... | \n",
+ " NaN | \n",
+ " 7 | \n",
+ " Personal Auto | \n",
+ " Personal L2 | \n",
+ " Offer1 | \n",
+ " Branch | \n",
+ " 707.925645 | \n",
+ " Four-Door Car | \n",
+ " Medsize | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
5 rows × 26 columns
\n",
+ "
"
+ ],
+ "text/plain": [
+ " unnamed:_0 customer state customer_lifetime_value response \\\n",
+ "0 0 DK49336 Arizona 4809.216960 No \n",
+ "1 1 KX64629 California 2228.525238 No \n",
+ "2 2 LZ68649 Washington 14947.917300 No \n",
+ "3 3 XL78013 Oregon 22332.439460 Yes \n",
+ "4 4 QA50777 Oregon 9025.067525 No \n",
+ "\n",
+ " coverage education effective_to_date employmentstatus gender ... \\\n",
+ "0 Basic College 2/18/11 Employed M ... \n",
+ "1 Basic College 1/18/11 Unemployed F ... \n",
+ "2 Basic Bachelor 2/10/11 Employed M ... \n",
+ "3 Extended College 1/11/11 Employed M ... \n",
+ "4 Premium Bachelor 1/17/11 Medical Leave F ... \n",
+ "\n",
+ " number_of_open_complaints number_of_policies policy_type policy \\\n",
+ "0 0.0 9 Corporate Auto Corporate L3 \n",
+ "1 0.0 1 Personal Auto Personal L3 \n",
+ "2 0.0 2 Personal Auto Personal L3 \n",
+ "3 0.0 2 Corporate Auto Corporate L3 \n",
+ "4 NaN 7 Personal Auto Personal L2 \n",
+ "\n",
+ " renew_offer_type sales_channel total_claim_amount vehicle_class \\\n",
+ "0 Offer3 Agent 292.800000 Four-Door Car \n",
+ "1 Offer4 Call Center 744.924331 Four-Door Car \n",
+ "2 Offer3 Call Center 480.000000 SUV \n",
+ "3 Offer2 Branch 484.013411 Four-Door Car \n",
+ "4 Offer1 Branch 707.925645 Four-Door Car \n",
+ "\n",
+ " vehicle_size vehicle_type \n",
+ "0 Medsize NaN \n",
+ "1 Medsize NaN \n",
+ "2 Medsize A \n",
+ "3 Medsize A \n",
+ "4 Medsize NaN \n",
+ "\n",
+ "[5 rows x 26 columns]"
]
- },
+ },
+ "execution_count": 3,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "df_filtered = df[(df[\"total_claim_amount\"] <= 1000) & (df[\"response\"] == \"Yes\")]\n",
+ "df.head()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "656f9950-4097-4529-8fc8-c8700b425590",
+ "metadata": {},
+ "source": [
+ "2. Using the original Dataframe, I filtered by \"responded yes\" and compared the average CLV to the TCA:\n",
+ " - For some reason, only women have a policy for \"Special Auto\";\n",
+ " - Women have a higher average CLV and lower average TCA for \"Personal auto\";\n",
+ " - The opposite verifies for \"Corporate Auto\"."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "id": "a7bd06d7-d40f-47b6-8b01-889f2d802f7a",
+ "metadata": {},
+ "outputs": [
{
- "cell_type": "markdown",
- "id": "b9be383e-5165-436e-80c8-57d4c757c8c3",
- "metadata": {
- "id": "b9be383e-5165-436e-80c8-57d4c757c8c3"
- },
- "source": [
- "2. Using the original Dataframe, analyze:\n",
- " - the average `monthly_premium` and/or customer lifetime value by `policy_type` and `gender` for customers who responded \"Yes\", and\n",
- " - compare these insights to `total_claim_amount` patterns, and discuss which segments appear most profitable or low-risk for the company."
+ "data": {
+ "text/plain": [
+ "policy_type gender\n",
+ "Corporate Auto F 7712.628736\n",
+ " M 7944.465414\n",
+ "Personal Auto F 8339.791842\n",
+ " M 7448.383281\n",
+ "Special Auto F 7691.584111\n",
+ "Name: customer_lifetime_value, dtype: float64"
]
- },
+ },
+ "execution_count": 11,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "responded_yes = df[df[\"response\"] == \"Yes\"]\n",
+ "df_filtered_clf = responded_yes.groupby(['policy_type', 'gender'])['customer_lifetime_value'].mean()\n",
+ "df_filtered_clf.head()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "id": "80ab0196-6930-46e5-97a6-b84d01e02f2b",
+ "metadata": {},
+ "outputs": [
{
- "cell_type": "markdown",
- "id": "7050f4ac-53c5-4193-a3c0-8699b87196f0",
- "metadata": {
- "id": "7050f4ac-53c5-4193-a3c0-8699b87196f0"
- },
- "source": [
- "3. Analyze the total number of customers who have policies in each state, and then filter the results to only include states where there are more than 500 customers."
+ "data": {
+ "text/plain": [
+ "policy_type gender\n",
+ "Corporate Auto F 433.738499\n",
+ " M 408.582459\n",
+ "Personal Auto F 452.965929\n",
+ " M 457.010178\n",
+ "Special Auto F 453.280164\n",
+ "Name: total_claim_amount, dtype: float64"
]
- },
+ },
+ "execution_count": 12,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "responded_yes = df[df[\"response\"] == \"Yes\"]\n",
+ "df_filtered_tca = responded_yes.groupby(['policy_type', 'gender'])['total_claim_amount'].mean()\n",
+ "df_filtered_tca.head()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "id": "113b0c04-defd-42f4-bfd1-560520696489",
+ "metadata": {},
+ "outputs": [
{
- "cell_type": "markdown",
- "id": "b60a4443-a1a7-4bbf-b78e-9ccdf9895e0d",
- "metadata": {
- "id": "b60a4443-a1a7-4bbf-b78e-9ccdf9895e0d"
- },
- "source": [
- "4. Find the maximum, minimum, and median customer lifetime value by education level and gender. Write your conclusions."
- ]
- },
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "state\n",
+ "California 3552\n",
+ "Oregon 2909\n",
+ "Arizona 1937\n",
+ "Nevada 993\n",
+ "Washington 888\n",
+ "Name: count, dtype: int64\n"
+ ]
+ }
+ ],
+ "source": [
+ "state_counts = df[\"state\"].value_counts()\n",
+ "\n",
+ "state_filtered = state_counts[state_counts > 500]\n",
+ "\n",
+ "print(state_counts)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "c4ff49cd-dc6e-47f1-b7e8-a2fe4e4959f9",
+ "metadata": {},
+ "source": [
+ "4. Insights:\n",
+ " - Master & Doctor have the highest minimum CLV for both genders, however, Highschool has it higher than both College and Bachelor (women's is higher,except);\n",
+ " - For max, mostly women have a higher max CLV (except for Highschool, which is much higher for men);\n",
+ " - The median is in the same 5k - 6k range for every group."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "id": "14c1ddee-4dfa-4500-8248-290c1ed77340",
+ "metadata": {},
+ "outputs": [
{
- "cell_type": "markdown",
- "id": "b42999f9-311f-481e-ae63-40a5577072c5",
- "metadata": {
- "id": "b42999f9-311f-481e-ae63-40a5577072c5"
- },
- "source": [
- "## Bonus"
+ "data": {
+ "text/plain": [
+ "education gender\n",
+ "Bachelor F 1904.000852\n",
+ " M 1898.007675\n",
+ "College F 1898.683686\n",
+ " M 1918.119700\n",
+ "Doctor F 2395.570000\n",
+ " M 2267.604038\n",
+ "High School or Below F 2144.921535\n",
+ " M 1940.981221\n",
+ "Master F 2417.777032\n",
+ " M 2272.307310\n",
+ "Name: customer_lifetime_value, dtype: float64"
]
- },
+ },
+ "execution_count": 7,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "df.groupby(['education', 'gender'])['customer_lifetime_value'].min()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "id": "f6440783-fb34-434d-9770-ad443668b389",
+ "metadata": {},
+ "outputs": [
{
- "cell_type": "markdown",
- "id": "81ff02c5-6584-4f21-a358-b918697c6432",
- "metadata": {
- "id": "81ff02c5-6584-4f21-a358-b918697c6432"
- },
- "source": [
- "5. The marketing team wants to analyze the number of policies sold by state and month. Present the data in a table where the months are arranged as columns and the states are arranged as rows."
+ "data": {
+ "text/plain": [
+ "education gender\n",
+ "Bachelor F 73225.95652\n",
+ " M 67907.27050\n",
+ "College F 61850.18803\n",
+ " M 61134.68307\n",
+ "Doctor F 44856.11397\n",
+ " M 32677.34284\n",
+ "High School or Below F 55277.44589\n",
+ " M 83325.38119\n",
+ "Master F 51016.06704\n",
+ " M 50568.25912\n",
+ "Name: customer_lifetime_value, dtype: float64"
]
- },
+ },
+ "execution_count": 8,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "df.groupby(['education', 'gender'])['customer_lifetime_value'].max()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "id": "7b364b40-57f2-4035-bcef-7aa1687135cc",
+ "metadata": {},
+ "outputs": [
{
- "cell_type": "markdown",
- "id": "b6aec097-c633-4017-a125-e77a97259cda",
- "metadata": {
- "id": "b6aec097-c633-4017-a125-e77a97259cda"
- },
- "source": [
- "6. Display a new DataFrame that contains the number of policies sold by month, by state, for the top 3 states with the highest number of policies sold.\n",
- "\n",
- "*Hint:*\n",
- "- *To accomplish this, you will first need to group the data by state and month, then count the number of policies sold for each group. Afterwards, you will need to sort the data by the count of policies sold in descending order.*\n",
- "- *Next, you will select the top 3 states with the highest number of policies sold.*\n",
- "- *Finally, you will create a new DataFrame that contains the number of policies sold by month for each of the top 3 states.*"
+ "data": {
+ "text/plain": [
+ "education gender\n",
+ "Bachelor F 5640.505303\n",
+ " M 5548.031892\n",
+ "College F 5623.611187\n",
+ " M 6005.847375\n",
+ "Doctor F 5332.462694\n",
+ " M 5577.669457\n",
+ "High School or Below F 6039.553187\n",
+ " M 6286.731006\n",
+ "Master F 5729.855012\n",
+ " M 5579.099207\n",
+ "Name: customer_lifetime_value, dtype: float64"
]
- },
+ },
+ "execution_count": 9,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "df.groupby(['education', 'gender'])['customer_lifetime_value'].median()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "b42999f9-311f-481e-ae63-40a5577072c5",
+ "metadata": {
+ "id": "b42999f9-311f-481e-ae63-40a5577072c5"
+ },
+ "source": [
+ "## Bonus"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "81ff02c5-6584-4f21-a358-b918697c6432",
+ "metadata": {
+ "id": "81ff02c5-6584-4f21-a358-b918697c6432"
+ },
+ "source": [
+ "5. The marketing team wants to analyze the number of policies sold by state and month. Present the data in a table where the months are arranged as columns and the states are arranged as rows."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 26,
+ "id": "70b311b5-b9d5-48b7-a248-3350bcb77f0d",
+ "metadata": {},
+ "outputs": [
{
- "cell_type": "markdown",
- "id": "ba975b8a-a2cf-4fbf-9f59-ebc381767009",
- "metadata": {
- "id": "ba975b8a-a2cf-4fbf-9f59-ebc381767009"
- },
- "source": [
- "7. The marketing team wants to analyze the effect of different marketing channels on the customer response rate.\n",
- "\n",
- "Hint: You can use melt to unpivot the data and create a table that shows the customer response rate (those who responded \"Yes\") by marketing channel."
+ "data": {
+ "text/plain": [
+ "month_of_purchase\n",
+ "January 5818\n",
+ "February 5092\n",
+ "Name: count, dtype: int64"
]
- },
+ },
+ "execution_count": 26,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "import pandas as pd\n",
+ "\n",
+ "df['effective_to_date'] = pd.to_datetime(df['effective_to_date'])\n",
+ "\n",
+ "df['month_of_purchase'] = df['effective_to_date'].dt.month_name()\n",
+ "\n",
+ "df['month_of_purchase'].value_counts()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 27,
+ "id": "eb352f08-1192-4969-9047-09b567a8f906",
+ "metadata": {},
+ "outputs": [
{
- "cell_type": "markdown",
- "id": "e4378d94-48fb-4850-a802-b1bc8f427b2d",
- "metadata": {
- "id": "e4378d94-48fb-4850-a802-b1bc8f427b2d"
- },
- "source": [
- "External Resources for Data Filtering: https://towardsdatascience.com/filtering-data-frames-in-pandas-b570b1f834b9"
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | month_of_purchase | \n",
+ " state | \n",
+ " February | \n",
+ " January | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " Arizona | \n",
+ " Personal Auto | \n",
+ " Personal Auto | \n",
+ "
\n",
+ " \n",
+ " | 1 | \n",
+ " California | \n",
+ " Personal Auto | \n",
+ " Personal Auto | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " Nevada | \n",
+ " Personal Auto | \n",
+ " Personal Auto | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " Oregon | \n",
+ " Personal Auto | \n",
+ " Personal Auto | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " Washington | \n",
+ " Personal Auto | \n",
+ " Personal Auto | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ "month_of_purchase state February January\n",
+ "0 Arizona Personal Auto Personal Auto\n",
+ "1 California Personal Auto Personal Auto\n",
+ "2 Nevada Personal Auto Personal Auto\n",
+ "3 Oregon Personal Auto Personal Auto\n",
+ "4 Washington Personal Auto Personal Auto"
]
- },
+ },
+ "execution_count": 27,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "pivoted_data = df.pivot_table(\n",
+ " index='state',\n",
+ " columns='month_of_purchase',\n",
+ " values='policy_type',\n",
+ " aggfunc=lambda x: x.value_counts().index[0]\n",
+ ").reset_index()\n",
+ "\n",
+ "pivoted_data.head()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "b6aec097-c633-4017-a125-e77a97259cda",
+ "metadata": {
+ "id": "b6aec097-c633-4017-a125-e77a97259cda"
+ },
+ "source": [
+ "6. Display a new DataFrame that contains the number of policies sold by month, by state, for the top 3 states with the highest number of policies sold.\n",
+ "\n",
+ "*Hint:*\n",
+ "- *To accomplish this, you will first need to group the data by state and month, then count the number of policies sold for each group. Afterwards, you will need to sort the data by the count of policies sold in descending order.*\n",
+ "- *Next, you will select the top 3 states with the highest number of policies sold.*\n",
+ "- *Finally, you will create a new DataFrame that contains the number of policies sold by month for each of the top 3 states.*"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 32,
+ "id": "4f561a05-e7a1-477b-9ea0-7f8ad0c33c56",
+ "metadata": {},
+ "outputs": [
{
- "cell_type": "code",
- "execution_count": null,
- "id": "449513f4-0459-46a0-a18d-9398d974c9ad",
- "metadata": {
- "id": "449513f4-0459-46a0-a18d-9398d974c9ad"
- },
- "outputs": [],
- "source": [
- "# your code goes here"
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | month_of_purchase | \n",
+ " January | \n",
+ " February | \n",
+ "
\n",
+ " \n",
+ " | state | \n",
+ " | \n",
+ " | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | Arizona | \n",
+ " 1008 | \n",
+ " 929 | \n",
+ "
\n",
+ " \n",
+ " | California | \n",
+ " 1918 | \n",
+ " 1634 | \n",
+ "
\n",
+ " \n",
+ " | Oregon | \n",
+ " 1565 | \n",
+ " 1344 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ "month_of_purchase January February\n",
+ "state \n",
+ "Arizona 1008 929\n",
+ "California 1918 1634\n",
+ "Oregon 1565 1344"
]
+ },
+ "execution_count": 32,
+ "metadata": {},
+ "output_type": "execute_result"
}
- ],
- "metadata": {
- "colab": {
- "provenance": []
- },
- "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.9.13"
- }
+ ],
+ "source": [
+ "import pandas as pd\n",
+ "\n",
+ "#identify the top 3 states\n",
+ "state_totals = df['state'].value_counts().head(3)\n",
+ "top_states = state_totals.index.tolist()\n",
+ "\n",
+ "#filter by the top 3 states\n",
+ "df_top_states = df[df['state'].isin(top_states)]\n",
+ "\n",
+ "#number of policies sold by month and state\n",
+ "monthly_sales = (\n",
+ " df_top_states\n",
+ " .groupby(['state', 'month_of_purchase'])\n",
+ " .size()\n",
+ " .reset_index(name='num_policies_sold')\n",
+ ")\n",
+ "\n",
+ "#define structure of pvt, months and columns and states on rows\n",
+ "monthly_sales_pivot = (\n",
+ " monthly_sales\n",
+ " .pivot(index='state', columns='month_of_purchase', values='num_policies_sold')\n",
+ " .fillna(0)\n",
+ " .astype(int)\n",
+ ")\n",
+ "\n",
+ "#ensure months are in the proper order\n",
+ "monthly_sales_pivot = monthly_sales_pivot.reindex(\n",
+ " columns=[m for m in month_order if m in monthly_sales_pivot.columns]\n",
+ ")\n",
+ "\n",
+ "monthly_sales_pivot"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "ba975b8a-a2cf-4fbf-9f59-ebc381767009",
+ "metadata": {
+ "id": "ba975b8a-a2cf-4fbf-9f59-ebc381767009"
+ },
+ "source": [
+ "7. The marketing team wants to analyze the effect of different marketing channels on the customer response rate.\n",
+ "\n",
+ "Hint: You can use melt to unpivot the data and create a table that shows the customer response rate (those who responded \"Yes\") by marketing channel."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "e4378d94-48fb-4850-a802-b1bc8f427b2d",
+ "metadata": {
+ "id": "e4378d94-48fb-4850-a802-b1bc8f427b2d"
+ },
+ "source": [
+ "External Resources for Data Filtering: https://towardsdatascience.com/filtering-data-frames-in-pandas-b570b1f834b9"
+ ]
+ }
+ ],
+ "metadata": {
+ "colab": {
+ "provenance": []
+ },
+ "kernelspec": {
+ "display_name": "Python [conda env:base] *",
+ "language": "python",
+ "name": "conda-base-py"
},
- "nbformat": 4,
- "nbformat_minor": 5
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.13.5"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
}