Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
365 changes: 365 additions & 0 deletions lab-dw-aggregating terminado.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,365 @@
{
"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": "94e3106f",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"marketing_df = pd.read_csv(\n",
" 'https://raw.githubusercontent.com/data-bootcamp-v4/data/main/marketing_customer_analysis.csv'\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "86d3b2e3",
"metadata": {},
"outputs": [],
"source": [
"marketing_df.columns = marketing_df.columns.str.lower().str.replace(' ', '_')\n",
"\n",
"text_cols = ['gender', 'education', 'policy_type', 'vehicle_class', 'sales_channel']\n",
"for col in text_cols:\n",
" marketing_df[col] = marketing_df[col].str.strip().str.title()\n",
"\n",
"marketing_df.drop_duplicates(inplace=True)"
]
},
{
"cell_type": "markdown",
"id": "f27a5103",
"metadata": {},
"source": [
"### Ejercicio 1\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "fb6a7a0e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" unnamed:_0 customer state customer_lifetime_value response \\\n",
"3 3 XL78013 Oregon 22332.439460 Yes \n",
"8 8 FM55990 California 5989.773931 Yes \n",
"15 15 CW49887 California 4626.801093 Yes \n",
"19 19 NJ54277 California 3746.751625 Yes \n",
"27 27 MQ68407 Oregon 4376.363592 Yes \n",
"\n",
" coverage education effective_to_date employmentstatus gender ... \\\n",
"3 Extended College 1/11/11 Employed M ... \n",
"8 Premium College 1/19/11 Employed M ... \n",
"15 Basic Master 1/16/11 Employed F ... \n",
"19 Extended College 2/26/11 Employed F ... \n",
"27 Premium Bachelor 2/28/11 Employed F ... \n",
"\n",
" number_of_open_complaints number_of_policies policy_type \\\n",
"3 0.0 2 Corporate Auto \n",
"8 0.0 1 Personal Auto \n",
"15 0.0 1 Special Auto \n",
"19 1.0 1 Personal Auto \n",
"27 0.0 1 Personal Auto \n",
"\n",
" policy renew_offer_type sales_channel total_claim_amount \\\n",
"3 Corporate L3 Offer2 Branch 484.013411 \n",
"8 Personal L1 Offer2 Branch 739.200000 \n",
"15 Special L1 Offer2 Branch 547.200000 \n",
"19 Personal L2 Offer2 Call Center 19.575683 \n",
"27 Personal L3 Offer2 Agent 60.036683 \n",
"\n",
" vehicle_class vehicle_size vehicle_type \n",
"3 Four-Door Car Medsize A \n",
"8 Sports Car Medsize NaN \n",
"15 Suv Medsize NaN \n",
"19 Two-Door Car Large A \n",
"27 Four-Door Car Medsize NaN \n",
"\n",
"[5 rows x 26 columns]\n",
"Número de clientes con bajo total_claim_amount y respuesta Yes: 1399\n"
]
}
],
"source": [
"low_claim_yes = marketing_df[\n",
" (marketing_df['total_claim_amount'] < 1000) &\n",
" (marketing_df['response'].str.strip().str.title() == 'Yes')\n",
"]\n",
"\n",
"print(low_claim_yes.head())\n",
"print(\"Número de clientes con bajo total_claim_amount y respuesta Yes:\", low_claim_yes.shape[0])"
]
},
{
"cell_type": "markdown",
"id": "a3900556",
"metadata": {},
"source": [
"### Ejercicio 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "34f77e6c",
"metadata": {},
"outputs": [],
"source": [
"yes_responders = marketing_df[marketing_df['response'].str.strip().str.title() == 'Yes']\n",
"\n",
"pivot_profit = yes_responders.pivot_table(\n",
" index='policy_type',\n",
" columns='gender',\n",
" values=['monthly_premium_auto', 'customer_lifetime_value'],\n",
" aggfunc='mean'\n",
").round(2)\n",
"\n",
"print(pivot_profit)"
]
},
{
"cell_type": "markdown",
"id": "5a5e42bc",
"metadata": {},
"source": [
"### Ejercicio 3"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9addc641",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" state total_customers\n",
"1 California 3552\n",
"3 Oregon 2909\n",
"0 Arizona 1937\n",
"2 Nevada 993\n",
"4 Washington 888\n"
]
}
],
"source": [
"customers_per_state = marketing_df.groupby('state')['customer'].count().reset_index(name='total_customers')\n",
"\n",
"states_large = customers_per_state[customers_per_state['total_customers'] > 500].sort_values(by='total_customers', ascending=False)\n",
"\n",
"print(states_large)"
]
},
{
"cell_type": "markdown",
"id": "1d2e8306",
"metadata": {},
"source": [
"### Ejercicio 4"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "80ac0936",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" max min median \n",
"gender F M F M F M\n",
"education \n",
"Bachelor 73225.96 67907.27 1904.00 1898.01 5640.51 5548.03\n",
"College 61850.19 61134.68 1898.68 1918.12 5623.61 6005.85\n",
"Doctor 44856.11 32677.34 2395.57 2267.60 5332.46 5577.67\n",
"High School Or Below 55277.45 83325.38 2144.92 1940.98 6039.55 6286.73\n",
"Master 51016.07 50568.26 2417.78 2272.31 5729.86 5579.10\n"
]
}
],
"source": [
"clv_stats = marketing_df.pivot_table(\n",
" index='education',\n",
" columns='gender',\n",
" values='customer_lifetime_value',\n",
" aggfunc=['max', 'min', 'median']\n",
").round(2)\n",
"\n",
"print(clv_stats)"
]
},
{
"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": null,
"id": "449513f4-0459-46a0-a18d-9398d974c9ad",
"metadata": {
"id": "449513f4-0459-46a0-a18d-9398d974c9ad"
},
"outputs": [],
"source": [
"# your code goes here"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"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.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}