From 69c6def450da1e907ea9328cd8e18fda2dbeba7f Mon Sep 17 00:00:00 2001 From: AnaP2020 Date: Sat, 13 Jun 2026 14:42:49 +0100 Subject: [PATCH 1/2] lab done aggregating --- lab-dw-aggregating.ipynb | 669 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 669 insertions(+) create mode 100644 lab-dw-aggregating.ipynb diff --git a/lab-dw-aggregating.ipynb b/lab-dw-aggregating.ipynb new file mode 100644 index 0000000..6bced07 --- /dev/null +++ b/lab-dw-aggregating.ipynb @@ -0,0 +1,669 @@ +{ + "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": 1, + "id": "449513f4-0459-46a0-a18d-9398d974c9ad", + "metadata": { + "id": "449513f4-0459-46a0-a18d-9398d974c9ad" + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "38026cd6", + "metadata": {}, + "outputs": [], + "source": [ + "marketing_customer_analysis_df = pd.read_csv(\"marketing_customer_analysis.csv\")" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "639e45ac", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Unnamed: 0 10910\n", + "Customer 9134\n", + "State 5\n", + "Customer Lifetime Value 8041\n", + "Response 2\n", + "Coverage 3\n", + "Education 5\n", + "Effective To Date 59\n", + "EmploymentStatus 5\n", + "Gender 2\n", + "Income 5694\n", + "Location Code 3\n", + "Marital Status 3\n", + "Monthly Premium Auto 202\n", + "Months Since Last Claim 36\n", + "Months Since Policy Inception 100\n", + "Number of Open Complaints 6\n", + "Number of Policies 9\n", + "Policy Type 3\n", + "Policy 9\n", + "Renew Offer Type 4\n", + "Sales Channel 4\n", + "Total Claim Amount 5106\n", + "Vehicle Class 6\n", + "Vehicle Size 3\n", + "Vehicle Type 1\n", + "dtype: int64" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "marketing_customer_analysis_df.nunique()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "ae991e79", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "RangeIndex: 10910 entries, 0 to 10909\n", + "Data columns (total 26 columns):\n", + " # Column Non-Null Count Dtype \n", + "--- ------ -------------- ----- \n", + " 0 Unnamed: 0 10910 non-null int64 \n", + " 1 Customer 10910 non-null str \n", + " 2 State 10279 non-null str \n", + " 3 Customer Lifetime Value 10910 non-null float64\n", + " 4 Response 10279 non-null str \n", + " 5 Coverage 10910 non-null str \n", + " 6 Education 10910 non-null str \n", + " 7 Effective To Date 10910 non-null str \n", + " 8 EmploymentStatus 10910 non-null str \n", + " 9 Gender 10910 non-null str \n", + " 10 Income 10910 non-null int64 \n", + " 11 Location Code 10910 non-null str \n", + " 12 Marital Status 10910 non-null str \n", + " 13 Monthly Premium Auto 10910 non-null int64 \n", + " 14 Months Since Last Claim 10277 non-null float64\n", + " 15 Months Since Policy Inception 10910 non-null int64 \n", + " 16 Number of Open Complaints 10277 non-null float64\n", + " 17 Number of Policies 10910 non-null int64 \n", + " 18 Policy Type 10910 non-null str \n", + " 19 Policy 10910 non-null str \n", + " 20 Renew Offer Type 10910 non-null str \n", + " 21 Sales Channel 10910 non-null str \n", + " 22 Total Claim Amount 10910 non-null float64\n", + " 23 Vehicle Class 10288 non-null str \n", + " 24 Vehicle Size 10288 non-null str \n", + " 25 Vehicle Type 5428 non-null str \n", + "dtypes: float64(4), int64(5), str(17)\n", + "memory usage: 3.4 MB\n" + ] + } + ], + "source": [ + "marketing_customer_analysis_df.info()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "bb255c2a", + "metadata": {}, + "outputs": [], + "source": [ + "marketing_customer_analysis_df.columns = marketing_customer_analysis_df.columns.str.lower().str.replace(' ', '_')" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "6c11085e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Index(['unnamed:_0', 'customer', 'state', 'customer_lifetime_value',\n", + " 'response', 'coverage', 'education', 'effective_to_date',\n", + " 'employmentstatus', 'gender', 'income', 'location_code',\n", + " 'marital_status', 'monthly_premium_auto', 'months_since_last_claim',\n", + " 'months_since_policy_inception', 'number_of_open_complaints',\n", + " 'number_of_policies', 'policy_type', 'policy', 'renew_offer_type',\n", + " 'sales_channel', 'total_claim_amount', 'vehicle_class', 'vehicle_size',\n", + " 'vehicle_type', 'Month'],\n", + " dtype='str')" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "marketing_customer_analysis_df.columns" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "fddc6a07", + "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", + "10844 10844 FM31768 Arizona 5979.724161 Yes \n", + "10852 10852 KZ80424 Washington 8382.478392 Yes \n", + "10872 10872 XT67997 California 5979.724161 Yes \n", + "10887 10887 BY78730 Oregon 8879.790017 Yes \n", + "10897 10897 MM70762 Arizona 9075.768214 Yes \n", + "\n", + " coverage education effective_to_date employmentstatus \\\n", + "3 Extended College 1/11/11 Employed \n", + "8 Premium College 1/19/11 Employed \n", + "15 Basic Master 1/16/11 Employed \n", + "19 Extended College 2/26/11 Employed \n", + "27 Premium Bachelor 2/28/11 Employed \n", + "... ... ... ... ... \n", + "10844 Extended High School or Below 2/7/11 Employed \n", + "10852 Basic Bachelor 1/27/11 Employed \n", + "10872 Extended High School or Below 2/7/11 Employed \n", + "10887 Basic High School or Below 2/3/11 Employed \n", + "10897 Basic Master 1/26/11 Employed \n", + "\n", + " gender ... number_of_open_complaints number_of_policies \\\n", + "3 M ... 0.0 2 \n", + "8 M ... 0.0 1 \n", + "15 F ... 0.0 1 \n", + "19 F ... 1.0 1 \n", + "27 F ... 0.0 1 \n", + "... ... ... ... ... \n", + "10844 F ... 0.0 3 \n", + "10852 M ... 0.0 2 \n", + "10872 F ... 0.0 3 \n", + "10887 F ... 0.0 7 \n", + "10897 M ... 0.0 8 \n", + "\n", + " policy_type policy renew_offer_type sales_channel \\\n", + "3 Corporate Auto Corporate L3 Offer2 Branch \n", + "8 Personal Auto Personal L1 Offer2 Branch \n", + "15 Special Auto Special L1 Offer2 Branch \n", + "19 Personal Auto Personal L2 Offer2 Call Center \n", + "27 Personal Auto Personal L3 Offer2 Agent \n", + "... ... ... ... ... \n", + "10844 Personal Auto Personal L1 Offer2 Agent \n", + "10852 Personal Auto Personal L2 Offer2 Call Center \n", + "10872 Personal Auto Personal L3 Offer2 Agent \n", + "10887 Special Auto Special L2 Offer1 Agent \n", + "10897 Personal Auto Personal L1 Offer1 Agent \n", + "\n", + " total_claim_amount vehicle_class vehicle_size vehicle_type \n", + "3 484.013411 Four-Door Car Medsize A \n", + "8 739.200000 Sports Car Medsize NaN \n", + "15 547.200000 SUV Medsize NaN \n", + "19 19.575683 Two-Door Car Large A \n", + "27 60.036683 Four-Door Car Medsize NaN \n", + "... ... ... ... ... \n", + "10844 547.200000 Four-Door Car Medsize NaN \n", + "10852 791.878042 NaN NaN A \n", + "10872 547.200000 Four-Door Car Medsize NaN \n", + "10887 528.200860 SUV Small A \n", + "10897 158.077504 Sports Car Medsize A \n", + "\n", + "[1399 rows x 26 columns]\n" + ] + } + ], + "source": [ + "#1. Creating a new dataframe \n", + "filtered_df = marketing_customer_analysis_df[\n", + " (marketing_customer_analysis_df[\"total_claim_amount\"] < 1000) & # Condition for low claim amount\n", + " (marketing_customer_analysis_df[\"response\"] == \"Yes\") # Condition for positive response\n", + "]\n", + "\n", + "# Display the filtered DataFrame\n", + "print(filtered_df)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "5df04e6a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average Metrics:\n", + " monthly_premium_auto customer_lifetime_value\n", + "policy_type gender \n", + "Corporate Auto F 94.30 7712.63\n", + " M 92.19 7944.47\n", + "Personal Auto F 99.00 8339.79\n", + " M 91.09 7448.38\n", + "Special Auto F 92.31 7691.58\n", + " M 86.34 8247.09\n", + "\n", + "Average Total Claim Amount:\n", + "policy_type gender\n", + "Corporate Auto F 433.74\n", + " M 408.58\n", + "Personal Auto F 452.97\n", + " M 457.01\n", + "Special Auto F 453.28\n", + " M 429.53\n", + "Name: total_claim_amount, dtype: float64\n" + ] + } + ], + "source": [ + "#2.\n", + "# Step 1: Filter the DataFrame for 'Yes' responses\n", + "responded_yes_df = marketing_customer_analysis_df[marketing_customer_analysis_df[\"response\"] == \"Yes\"]\n", + "\n", + "# Step 2: Calculate averages\n", + "average_metrics = responded_yes_df.groupby([\"policy_type\", \"gender\"]).agg({\n", + " \"monthly_premium_auto\": \"mean\",\n", + " \"customer_lifetime_value\": \"mean\"\n", + "}).round(2)\n", + "\n", + "# Step 3: Analyze `total_claim_amount`\n", + "average_claims = responded_yes_df.groupby([\"policy_type\", \"gender\"])[\"total_claim_amount\"].mean().round(2)\n", + "\n", + "# Display results\n", + "print(\"Average Metrics:\")\n", + "print(average_metrics)\n", + "\n", + "print(\"\\nAverage Total Claim Amount:\")\n", + "print(average_claims)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "24217385", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " state customer_count\n", + "0 Arizona 1937\n", + "1 California 3552\n", + "2 Nevada 993\n", + "3 Oregon 2909\n", + "4 Washington 888\n" + ] + } + ], + "source": [ + "#3\n", + "# Group by 'state' and count the number of customers in each state\n", + "customers_per_state = marketing_customer_analysis_df.groupby(\"state\").size().reset_index(name=\"customer_count\")\n", + "\n", + "# Filter the states to include only those with more than 500 customers\n", + "states_with_large_customer_base = customers_per_state[customers_per_state[\"customer_count\"] > 500]\n", + "\n", + "print(states_with_large_customer_base)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "8a5f2ac3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " max min median\n", + "education gender \n", + "Bachelor F 73225.95652 1904.000852 5640.505303\n", + " M 67907.27050 1898.007675 5548.031892\n", + "College F 61850.18803 1898.683686 5623.611187\n", + " M 61134.68307 1918.119700 6005.847375\n", + "Doctor F 44856.11397 2395.570000 5332.462694\n", + " M 32677.34284 2267.604038 5577.669457\n", + "High School or Below F 55277.44589 2144.921535 6039.553187\n", + " M 83325.38119 1940.981221 6286.731006\n", + "Master F 51016.06704 2417.777032 5729.855012\n", + " M 50568.25912 2272.307310 5579.099207\n" + ] + } + ], + "source": [ + "#4\n", + "# Group by 'education' and 'gender' and calculate the required statistics\n", + "clv_stats_by_group = marketing_customer_analysis_df.groupby([\"education\", \"gender\"])[\"customer_lifetime_value\"].agg([\"max\", \"min\", \"median\"])\n", + "\n", + "# Display the results\n", + "print(clv_stats_by_group)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "25abcb0a", + "metadata": {}, + "outputs": [], + "source": [ + "#Conclusions:\n", + "#Higher CLV in males with lower education levels could suggest opportunities for engagement initiatives targeting this demographic with educational content, \n", + "# up-sell opportunities, or loyalty programs.\n", + "#Differences in median and maximum values between genders can guide gender-focused campaign elements, \n", + "#ensuring communication is tailored to engage effectively with each group." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "c48d7125", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "month January February March April May June July August \\\n", + "state \n", + "Arizona 1008 929 NaN NaN NaN NaN NaN NaN \n", + "California 1918 1634 NaN NaN NaN NaN NaN NaN \n", + "Nevada 551 442 NaN NaN NaN NaN NaN NaN \n", + "Oregon 1565 1344 NaN NaN NaN NaN NaN NaN \n", + "Washington 463 425 NaN NaN NaN NaN NaN NaN \n", + "\n", + "month September October November December \n", + "state \n", + "Arizona NaN NaN NaN NaN \n", + "California NaN NaN NaN NaN \n", + "Nevada NaN NaN NaN NaN \n", + "Oregon NaN NaN NaN NaN \n", + "Washington NaN NaN NaN NaN \n" + ] + } + ], + "source": [ + "#5\n", + "# Convert the 'Effective To Date' column to datetime\n", + "marketing_customer_analysis_df[\"effective_to_date\"] = pd.to_datetime(marketing_customer_analysis_df[\"effective_to_date\"])\n", + "\n", + "# Extract the month name for each entry\n", + "marketing_customer_analysis_df[\"month\"] = marketing_customer_analysis_df[\"effective_to_date\"].dt.month_name()\n", + "\n", + "# Create a pivot table\n", + "policies_by_state_and_month = marketing_customer_analysis_df.pivot_table(\n", + " index=\"state\",\n", + " columns=\"month\",\n", + " values=\"policy\", \n", + " aggfunc=\"count\"\n", + ")\n", + "\n", + "# Reorder the columns to maintain calendar order\n", + "policies_by_state_and_month = policies_by_state_and_month.reindex(columns=[\n", + " \"January\", \"February\", \"March\", \"April\", \"May\", \"June\", \n", + " \"July\", \"August\", \"September\", \"October\", \"November\", \"December\"\n", + "])\n", + "\n", + "# Display the pivot table\n", + "print(policies_by_state_and_month)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "b03eb472", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " state month policies_sold Month\n", + "0 Arizona February 929 February\n", + "1 Arizona January 1008 January\n", + "2 California February 1634 February\n", + "3 California January 1918 January\n", + "6 Oregon February 1344 February\n", + "7 Oregon January 1565 January\n" + ] + } + ], + "source": [ + "#6\n", + "# Group by state and month to count the number of policies sold\n", + "policies_by_state_month = marketing_customer_analysis_df.groupby([\"state\", \"month\"]).size().reset_index(name=\"policies_sold\")\n", + "\n", + "# Calculate total policies sold by state\n", + "total_policies_by_state = policies_by_state_month.groupby(\"state\")[\"policies_sold\"].sum().reset_index()\n", + "\n", + "# Sort and get the top 3 states with the highest number of policies sold\n", + "top_3_states = total_policies_by_state.sort_values(by=\"policies_sold\", ascending=False).head(3)[\"state\"]\n", + "\n", + "# Filter for the top 3 states\n", + "top_3_policies_by_month = policies_by_state_month[policies_by_state_month[\"state\"].isin(top_3_states)]\n", + "\n", + "# Optional: reorder the months to ensure they appear in calendar order\n", + "calendar_months = [\"January\", \"February\", \"March\", \"April\", \"May\", \"June\", \n", + " \"July\", \"August\", \"September\", \"October\", \"November\", \"December\"]\n", + "top_3_policies_by_month['Month'] = pd.Categorical(top_3_policies_by_month[\"month\"],\n", + " categories=calendar_months,\n", + " ordered=True)\n", + "\n", + "# Display the resulting DataFrame\n", + "print(top_3_policies_by_month.sort_values(by=[\"state\", \"month\"]))" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "c77113d7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " sales_channel yes_count total_count response_rate\n", + "0 Agent 742 4121 18.005339\n", + "1 Branch 326 3022 10.787558\n", + "2 Call Center 221 2141 10.322279\n", + "3 Web 177 1626 10.885609\n" + ] + } + ], + "source": [ + "#7\n", + "# Filter and calculate the number of \"Yes\" responses per channel\n", + "yes_responses = marketing_customer_analysis_df[marketing_customer_analysis_df[\"response\"] == \"Yes\"].groupby(\"sales_channel\").size().reset_index(name=\"yes_count\")\n", + "\n", + "# Calculate total responses per channel\n", + "total_responses = marketing_customer_analysis_df.groupby(\"sales_channel\").size().reset_index(name=\"total_count\")\n", + "\n", + "# Merge the dataframes based on \"sales_channel\"\n", + "response_data = pd.merge(yes_responses, total_responses, on=\"sales_channel\")\n", + "\n", + "# Calculate the response rate\n", + "response_data[\"response_rate\"] = (response_data[\"yes_count\"] / response_data[\"total_count\"]) * 100\n", + "\n", + "# Display the resulting DataFrame\n", + "print(response_data)\n" + ] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "base", + "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.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 30e30c1e5cfe4743c70a933f3ecb6537ceb1a949 Mon Sep 17 00:00:00 2001 From: AnaP2020 Date: Sat, 13 Jun 2026 14:50:13 +0100 Subject: [PATCH 2/2] Done again --- lab-dw-aggregating.ipynb | 43 +++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/lab-dw-aggregating.ipynb b/lab-dw-aggregating.ipynb index 6bced07..608159d 100644 --- a/lab-dw-aggregating.ipynb +++ b/lab-dw-aggregating.ipynb @@ -562,7 +562,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "id": "b03eb472", "metadata": {}, "outputs": [ @@ -594,7 +594,7 @@ "# Filter for the top 3 states\n", "top_3_policies_by_month = policies_by_state_month[policies_by_state_month[\"state\"].isin(top_3_states)]\n", "\n", - "# Optional: reorder the months to ensure they appear in calendar order\n", + "#reorder the months to ensure they appear in calendar order\n", "calendar_months = [\"January\", \"February\", \"March\", \"April\", \"May\", \"June\", \n", " \"July\", \"August\", \"September\", \"October\", \"November\", \"December\"]\n", "top_3_policies_by_month['Month'] = pd.Categorical(top_3_policies_by_month[\"month\"],\n", @@ -607,7 +607,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 24, "id": "c77113d7", "metadata": {}, "outputs": [ @@ -615,30 +615,37 @@ "name": "stdout", "output_type": "stream", "text": [ - " sales_channel yes_count total_count response_rate\n", - "0 Agent 742 4121 18.005339\n", - "1 Branch 326 3022 10.787558\n", - "2 Call Center 221 2141 10.322279\n", - "3 Web 177 1626 10.885609\n" + " state month Policies_Sold\n", + "1 Arizona January 1008\n", + "0 Arizona February 929\n", + "3 California January 1918\n", + "2 California February 1634\n", + "7 Oregon January 1565\n", + "6 Oregon February 1344\n" ] } ], "source": [ "#7\n", - "# Filter and calculate the number of \"Yes\" responses per channel\n", - "yes_responses = marketing_customer_analysis_df[marketing_customer_analysis_df[\"response\"] == \"Yes\"].groupby(\"sales_channel\").size().reset_index(name=\"yes_count\")\n", "\n", - "# Calculate total responses per channel\n", - "total_responses = marketing_customer_analysis_df.groupby(\"sales_channel\").size().reset_index(name=\"total_count\")\n", + "#Group by State and Month to calculate the number of policies sold\n", + "policies_by_state_month = marketing_customer_analysis_df.groupby([\"state\", \"month\"]).size().reset_index(name='Policies_Sold')\n", "\n", - "# Merge the dataframes based on \"sales_channel\"\n", - "response_data = pd.merge(yes_responses, total_responses, on=\"sales_channel\")\n", + "# Step 2: Calculate total policies sold by state\n", + "total_policies_by_state = policies_by_state_month.groupby(\"state\")['Policies_Sold'].sum().reset_index()\n", "\n", - "# Calculate the response rate\n", - "response_data[\"response_rate\"] = (response_data[\"yes_count\"] / response_data[\"total_count\"]) * 100\n", + "# Step 3: Sort and select the top 3 states with the highest number of policies sold\n", + "top_3_states = total_policies_by_state.sort_values(by='Policies_Sold', ascending=False).head(3)[\"state\"]\n", "\n", - "# Display the resulting DataFrame\n", - "print(response_data)\n" + "# Step 4: Filter the policies_by_state_month DataFrame for only the top 3 states\n", + "top_3_policies_by_month = policies_by_state_month[policies_by_state_month[\"state\"].isin(top_3_states)]\n", + "\n", + "top_3_policies_by_month[\"month\"] = pd.Categorical(top_3_policies_by_month[\"month\"],\n", + " categories=calendar_months,\n", + " ordered=True)\n", + "\n", + "# Display the final DataFrame\n", + "print(top_3_policies_by_month.sort_values(by=[\"state\", \"month\"]))" ] } ],