Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab solved #336

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
175 changes: 152 additions & 23 deletions .ipynb_checkpoints/lab-dw-aggregating-checkpoint.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,81 +3,97 @@
{
"cell_type": "markdown",
"id": "31969215-2a90-4d8b-ac36-646a7ae13744",
"metadata": {},
"metadata": {
"id": "31969215-2a90-4d8b-ac36-646a7ae13744"
},
"source": [
"# Lab | Data Aggregation and Filtering"
]
},
{
"cell_type": "markdown",
"id": "a8f08a52-bec0-439b-99cc-11d3809d8b5d",
"metadata": {},
"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 performing first data cleaning, formatting, and structuring."
"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": {},
"metadata": {
"id": "9c98ddc5-b041-4c94-ada1-4dfee5c98e50"
},
"source": [
"1. Create a new DataFrame that only includes customers who have a total_claim_amount greater than $1,000 and have a response of \"Yes\" to the last marketing campaign."
]
},
{
"cell_type": "markdown",
"id": "b9be383e-5165-436e-80c8-57d4c757c8c3",
"metadata": {},
"metadata": {
"id": "b9be383e-5165-436e-80c8-57d4c757c8c3"
},
"source": [
"2. Using the original dataframe, analyze the average total_claim_amount by each policy type and gender for customers who have responded \"Yes\" to the last marketing campaign. Write your conclusions."
"2. Using the original Dataframe, analyze the average total_claim_amount by each policy type and gender for customers who have responded \"Yes\" to the last marketing campaign. Write your conclusions."
]
},
{
"cell_type": "markdown",
"id": "7050f4ac-53c5-4193-a3c0-8699b87196f0",
"metadata": {},
"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": {},
"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": {},
"metadata": {
"id": "b42999f9-311f-481e-ae63-40a5577072c5"
},
"source": [
"## Bonus"
]
},
{
"cell_type": "markdown",
"id": "81ff02c5-6584-4f21-a358-b918697c6432",
"metadata": {},
"metadata": {
"id": "81ff02c5-6584-4f21-a358-b918697c6432"
},
"source": [
"5. The marketing team wants to analyze the number of policies sold by state and month.\n",
"\n",
"Present the data in a table where the months are arranged as columns and the states are arranged as rows."
"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": {},
"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",
"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",
"*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.*"
Expand All @@ -86,7 +102,9 @@
{
"cell_type": "markdown",
"id": "ba975b8a-a2cf-4fbf-9f59-ebc381767009",
"metadata": {},
"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",
Expand All @@ -96,27 +114,138 @@
{
"cell_type": "markdown",
"id": "e4378d94-48fb-4850-a802-b1bc8f427b2d",
"metadata": {},
"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,
"execution_count": null,
"id": "449513f4-0459-46a0-a18d-9398d974c9ad",
"metadata": {
"id": "449513f4-0459-46a0-a18d-9398d974c9ad"
},
"outputs": [],
"source": [
"import pandas as pd\n",
"url=\"https://raw.githubusercontent.com/data-bootcamp-v4/data/main/marketing_customer_analysis.csv\"\n",
"customers=pd.read_csv(url)\n",
"customers.head()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "223e5c2f-59b7-47a9-9455-02665482c3a3",
"metadata": {},
"outputs": [],
"source": [
"#1. Create a new DataFrame that only includes customers who have a total_claim_amount greater than $1,000 and have a response of \"Yes\" to the last marketing campaign.\n",
"condition1= (customers[\"Response\"]==\"Yes\") & (customers[\"Total Claim Amount\"]>1000)\n",
"customer_condition1=customers[condition1]\n",
"customer_condition1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7b48a257-8079-45fc-bac5-b74d1aab1ff5",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"#2. Using the original Dataframe, analyze the average total_claim_amount by each policy type and gender for customers who have responded \"Yes\" to the last marketing campaign. Write your conclusions.\n",
"condition2=customers[\"Response\"]==\"Yes\"\n",
"customers_condition2=customers[condition2]\n",
"customers_condition2.groupby([\"Policy Type\", \"Gender\"]).agg({\"Total Claim Amount\":[\"mean\"]})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "710b8ce6-54aa-4d24-b206-14fa85d69979",
"metadata": {},
"outputs": [],
"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.\n",
"customers_state=customers.groupby(\"State\").agg({\"Customer\":\"count\"})\n",
"condition2=customers_state[\"Customer\"]> 500\n",
"customers_state2=customers_state[condition2]\n",
"customers_state2\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5a82014d-6a07-4e27-8ae5-017a398f705f",
"metadata": {},
"outputs": [],
"source": [
"#4.Find the maximum, minimum, and median customer lifetime value by education level and gender. Write your conclusions.\n",
"customers_lifetime=customers.groupby([\"Education\", \"Gender\"]).agg({\"Customer Lifetime Value\": [\"median\",\"max\",\"min\"]})\n",
"customers_lifetime"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39a1b186-b5f8-4690-a426-c35ef302419e",
"metadata": {},
"outputs": [],
"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.\n",
"customers['Effective To Date'] = pd.to_datetime(customers['Effective To Date'])\n",
"customers.dtypes\n",
"customers[\"Month\"]=customers[\"Effective To Date\"].dt.month\n",
"pivot=customers.pivot_table(index=\"Month\",columns=\"State\", values=\"Customer\", aggfunc=\"count\")\n",
"pivot"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bd39d5ec-709a-4df8-83cb-e5333766f20c",
"metadata": {},
"outputs": [],
"source": [
"#6.Filter the top_state\n",
"customers[\"Number of Policies\"]=customers[\"Number of Policies\"].astype(int)\n",
"top_state=((pd.DataFrame(customers.groupby(\"State\")[\"Number of Policies\"].sum())).sort_values(\"Number of Policies\",ascending=False)).head(3)\n",
"top_state=top_state.reset_index()\n",
"top_state_list=top_state[\"State\"].tolist()\n",
"top_state_list\n",
"#6. Create the table with the top_state\n",
"condition6=customers[\"State\"].isin(top_state_list)\n",
"customers6=customers[condition6]\n",
"customers6=customers6.groupby([\"State\", \"Month\"]).agg({\"Customer\":\"count\"}).sort_values(by=\"Customer\", ascending=False)\n",
"customers6"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8aea984c-307d-4035-9d61-ac19e3379881",
"metadata": {},
"outputs": [],
"source": [
"#7.The marketing team wants to analyze the effect of different marketing channels on the customer response rate.\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.\n",
"\n",
"response_rate = customers.groupby('Sales Channel')['Response'].value_counts(normalize=True).unstack()\n",
"response_rate"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": ".venv",
"language": "python",
"name": "python3"
"name": ".venv"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -128,7 +257,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.1"
}
},
"nbformat": 4,
Expand Down
Loading