diff --git a/lab-dw-aggregating.ipynb b/lab-dw-aggregating.ipynb
index fadd718..2a7e2a8 100644
--- a/lab-dw-aggregating.ipynb
+++ b/lab-dw-aggregating.ipynb
@@ -1,165 +1,776 @@
{
- "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": "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."
- ]
- },
+ "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": "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": "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": "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"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "id": "449513f4-0459-46a0-a18d-9398d974c9ad",
+ "metadata": {
+ "id": "449513f4-0459-46a0-a18d-9398d974c9ad"
+ },
+ "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/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",
+ " income | \n",
+ " location_code | \n",
+ " marital_status | \n",
+ " monthly_premium_auto | \n",
+ " months_since_last_claim | \n",
+ " months_since_policy_inception | \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",
+ " 3 | \n",
+ " XL78013 | \n",
+ " Oregon | \n",
+ " 22332.439460 | \n",
+ " Yes | \n",
+ " Extended | \n",
+ " College | \n",
+ " 1/11/11 | \n",
+ " Employed | \n",
+ " M | \n",
+ " 49078 | \n",
+ " Suburban | \n",
+ " Single | \n",
+ " 97 | \n",
+ " 10.0 | \n",
+ " 3 | \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",
+ " | 1 | \n",
+ " 8 | \n",
+ " FM55990 | \n",
+ " California | \n",
+ " 5989.773931 | \n",
+ " Yes | \n",
+ " Premium | \n",
+ " College | \n",
+ " 1/19/11 | \n",
+ " Employed | \n",
+ " M | \n",
+ " 66839 | \n",
+ " Suburban | \n",
+ " Single | \n",
+ " 154 | \n",
+ " 33.0 | \n",
+ " 24 | \n",
+ " 0.0 | \n",
+ " 1 | \n",
+ " Personal Auto | \n",
+ " Personal L1 | \n",
+ " Offer2 | \n",
+ " Branch | \n",
+ " 739.200000 | \n",
+ " Sports Car | \n",
+ " Medsize | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " 15 | \n",
+ " CW49887 | \n",
+ " California | \n",
+ " 4626.801093 | \n",
+ " Yes | \n",
+ " Basic | \n",
+ " Master | \n",
+ " 1/16/11 | \n",
+ " Employed | \n",
+ " F | \n",
+ " 79487 | \n",
+ " Suburban | \n",
+ " Divorced | \n",
+ " 114 | \n",
+ " 20.0 | \n",
+ " 87 | \n",
+ " 0.0 | \n",
+ " 1 | \n",
+ " Special Auto | \n",
+ " Special L1 | \n",
+ " Offer2 | \n",
+ " Branch | \n",
+ " 547.200000 | \n",
+ " SUV | \n",
+ " Medsize | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " 19 | \n",
+ " NJ54277 | \n",
+ " California | \n",
+ " 3746.751625 | \n",
+ " Yes | \n",
+ " Extended | \n",
+ " College | \n",
+ " 2/26/11 | \n",
+ " Employed | \n",
+ " F | \n",
+ " 41479 | \n",
+ " Rural | \n",
+ " Married | \n",
+ " 94 | \n",
+ " 14.0 | \n",
+ " 38 | \n",
+ " 1.0 | \n",
+ " 1 | \n",
+ " Personal Auto | \n",
+ " Personal L2 | \n",
+ " Offer2 | \n",
+ " Call Center | \n",
+ " 19.575683 | \n",
+ " Two-Door Car | \n",
+ " Large | \n",
+ " A | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " 27 | \n",
+ " MQ68407 | \n",
+ " Oregon | \n",
+ " 4376.363592 | \n",
+ " Yes | \n",
+ " Premium | \n",
+ " Bachelor | \n",
+ " 2/28/11 | \n",
+ " Employed | \n",
+ " F | \n",
+ " 63774 | \n",
+ " Rural | \n",
+ " Divorced | \n",
+ " 111 | \n",
+ " 18.0 | \n",
+ " 63 | \n",
+ " 0.0 | \n",
+ " 1 | \n",
+ " Personal Auto | \n",
+ " Personal L3 | \n",
+ " Offer2 | \n",
+ " Agent | \n",
+ " 60.036683 | \n",
+ " Four-Door Car | \n",
+ " Medsize | \n",
+ " NaN | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " unnamed:_0 customer state ... vehicle_class vehicle_size vehicle_type\n",
+ "0 3 XL78013 Oregon ... Four-Door Car Medsize A\n",
+ "1 8 FM55990 California ... Sports Car Medsize NaN\n",
+ "2 15 CW49887 California ... SUV Medsize NaN\n",
+ "3 19 NJ54277 California ... Two-Door Car Large A\n",
+ "4 27 MQ68407 Oregon ... Four-Door Car Medsize NaN\n",
+ "\n",
+ "[5 rows x 26 columns]"
]
- },
+ },
+ "execution_count": 6,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# your code goes here\n",
+ "#Step: 1\n",
+ "\n",
+ "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.columns = (\n",
+ " df.columns\n",
+ " .str.strip() # remove leading/trailing spaces\n",
+ " .str.lower()\n",
+ " .str.replace(\" \", \"_\") # replace internal spaces with _\n",
+ ")\n",
+ "df.columns\n",
+ "\n",
+ "df_filtered = df[(df[\"total_claim_amount\"] < 1000) & (df[\"response\"] == \"Yes\")]\n",
+ "\n",
+ "df_filtered = df_filtered.reset_index(drop = True)\n",
+ "df_filtered.head()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "id": "018beb7d-265b-4660-b61f-b76e832582e2",
+ "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/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " policy_type | \n",
+ " gender | \n",
+ " avg_monthly_income | \n",
+ " avg_clv | \n",
+ " avg_total_claim | \n",
+ " count | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " Corporate Auto | \n",
+ " F | \n",
+ " 94.301775 | \n",
+ " 7712.628736 | \n",
+ " 433.738499 | \n",
+ " 169 | \n",
+ "
\n",
+ " \n",
+ " | 1 | \n",
+ " Corporate Auto | \n",
+ " M | \n",
+ " 92.188312 | \n",
+ " 7944.465414 | \n",
+ " 408.582459 | \n",
+ " 154 | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " Personal Auto | \n",
+ " F | \n",
+ " 98.998148 | \n",
+ " 8339.791842 | \n",
+ " 452.965929 | \n",
+ " 540 | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " Personal Auto | \n",
+ " M | \n",
+ " 91.085821 | \n",
+ " 7448.383281 | \n",
+ " 457.010178 | \n",
+ " 536 | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " Special Auto | \n",
+ " F | \n",
+ " 92.314286 | \n",
+ " 7691.584111 | \n",
+ " 453.280164 | \n",
+ " 35 | \n",
+ "
\n",
+ " \n",
+ " | 5 | \n",
+ " Special Auto | \n",
+ " M | \n",
+ " 86.343750 | \n",
+ " 8247.088702 | \n",
+ " 429.527942 | \n",
+ " 32 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " policy_type gender ... avg_total_claim count\n",
+ "0 Corporate Auto F ... 433.738499 169\n",
+ "1 Corporate Auto M ... 408.582459 154\n",
+ "2 Personal Auto F ... 452.965929 540\n",
+ "3 Personal Auto M ... 457.010178 536\n",
+ "4 Special Auto F ... 453.280164 35\n",
+ "5 Special Auto M ... 429.527942 32\n",
+ "\n",
+ "[6 rows x 6 columns]"
]
- },
+ },
+ "execution_count": 7,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Step: 2 \n",
+ "\n",
+ "df_yes = df[df[\"response\"] == \"Yes\"]\n",
+ "\n",
+ "summary = df_yes.groupby([\"policy_type\", \"gender\"]).agg(\n",
+ " avg_monthly_income = (\"monthly_premium_auto\", \"mean\"),\n",
+ " avg_clv = (\"customer_lifetime_value\", \"mean\"),\n",
+ " avg_total_claim = (\"total_claim_amount\", \"mean\"),\n",
+ " count = (\"customer\", \"count\")\n",
+ ").reset_index()\n",
+ "\n",
+ "summary"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "id": "df8a23d4-5979-4cf3-88e7-8f852ade4f64",
+ "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",
+ " | \n",
+ " state | \n",
+ " customer_count | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " California | \n",
+ " 3552 | \n",
+ "
\n",
+ " \n",
+ " | 1 | \n",
+ " Oregon | \n",
+ " 2909 | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " Arizona | \n",
+ " 1937 | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " Nevada | \n",
+ " 993 | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " Washington | \n",
+ " 888 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " state customer_count\n",
+ "0 California 3552\n",
+ "1 Oregon 2909\n",
+ "2 Arizona 1937\n",
+ "3 Nevada 993\n",
+ "4 Washington 888"
]
- },
+ },
+ "execution_count": 11,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Step 3: \n",
+ "\n",
+ "state_counts = df['state'].value_counts().reset_index()\n",
+ "state_counts.columns = ['state', 'customer_count']\n",
+ "\n",
+ "states_over_500 = state_counts[state_counts['customer_count'] > 500]\n",
+ "states_over_500"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "id": "c3f03db9-8f98-40d9-b1ba-88d942188d45",
+ "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",
+ " | \n",
+ " education | \n",
+ " gender | \n",
+ " min | \n",
+ " median | \n",
+ " max | \n",
+ " count | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " Bachelor | \n",
+ " F | \n",
+ " 1904.000852 | \n",
+ " 5640.505303 | \n",
+ " 73225.95652 | \n",
+ " 1700 | \n",
+ "
\n",
+ " \n",
+ " | 1 | \n",
+ " Bachelor | \n",
+ " M | \n",
+ " 1898.007675 | \n",
+ " 5548.031892 | \n",
+ " 67907.27050 | \n",
+ " 1572 | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " College | \n",
+ " F | \n",
+ " 1898.683686 | \n",
+ " 5623.611187 | \n",
+ " 61850.18803 | \n",
+ " 1608 | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " College | \n",
+ " M | \n",
+ " 1918.119700 | \n",
+ " 6005.847375 | \n",
+ " 61134.68307 | \n",
+ " 1596 | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " Doctor | \n",
+ " F | \n",
+ " 2395.570000 | \n",
+ " 5332.462694 | \n",
+ " 44856.11397 | \n",
+ " 200 | \n",
+ "
\n",
+ " \n",
+ " | 5 | \n",
+ " Doctor | \n",
+ " M | \n",
+ " 2267.604038 | \n",
+ " 5577.669457 | \n",
+ " 32677.34284 | \n",
+ " 201 | \n",
+ "
\n",
+ " \n",
+ " | 6 | \n",
+ " High School or Below | \n",
+ " F | \n",
+ " 2144.921535 | \n",
+ " 6039.553187 | \n",
+ " 55277.44589 | \n",
+ " 1590 | \n",
+ "
\n",
+ " \n",
+ " | 7 | \n",
+ " High School or Below | \n",
+ " M | \n",
+ " 1940.981221 | \n",
+ " 6286.731006 | \n",
+ " 83325.38119 | \n",
+ " 1556 | \n",
+ "
\n",
+ " \n",
+ " | 8 | \n",
+ " Master | \n",
+ " F | \n",
+ " 2417.777032 | \n",
+ " 5729.855012 | \n",
+ " 51016.06704 | \n",
+ " 475 | \n",
+ "
\n",
+ " \n",
+ " | 9 | \n",
+ " Master | \n",
+ " M | \n",
+ " 2272.307310 | \n",
+ " 5579.099207 | \n",
+ " 50568.25912 | \n",
+ " 412 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " education gender min median max count\n",
+ "0 Bachelor F 1904.000852 5640.505303 73225.95652 1700\n",
+ "1 Bachelor M 1898.007675 5548.031892 67907.27050 1572\n",
+ "2 College F 1898.683686 5623.611187 61850.18803 1608\n",
+ "3 College M 1918.119700 6005.847375 61134.68307 1596\n",
+ "4 Doctor F 2395.570000 5332.462694 44856.11397 200\n",
+ "5 Doctor M 2267.604038 5577.669457 32677.34284 201\n",
+ "6 High School or Below F 2144.921535 6039.553187 55277.44589 1590\n",
+ "7 High School or Below M 1940.981221 6286.731006 83325.38119 1556\n",
+ "8 Master F 2417.777032 5729.855012 51016.06704 475\n",
+ "9 Master M 2272.307310 5579.099207 50568.25912 412"
]
+ },
+ "execution_count": 10,
+ "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": [
+ "# Step 4:\n",
+ "\n",
+ "df[\"customer_lifetime_value\"] = pd.to_numeric(df[\"customer_lifetime_value\"], errors = \"coerce\")\n",
+ "\n",
+ "clv_stats = df.groupby([\"education\", \"gender\"])[\"customer_lifetime_value\"].agg([\"min\", \"median\", \"max\", \"count\"]).reset_index()\n",
+ "clv_stats"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "86f96315-2bd5-49bc-b054-b5d361d3330f",
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "colab": {
+ "provenance": []
+ },
+ "kernelspec": {
+ "display_name": "Python 3 (ipykernel)",
+ "language": "python",
+ "name": "python3"
},
- "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
}