diff --git a/.DS_Store b/.DS_Store
new file mode 100644
index 00000000..32b46836
Binary files /dev/null and b/.DS_Store differ
diff --git a/lab-dw-pandas.ipynb b/lab-dw-pandas.ipynb
index fbd46831..81058958 100644
--- a/lab-dw-pandas.ipynb
+++ b/lab-dw-pandas.ipynb
@@ -3,7 +3,9 @@
{
"cell_type": "markdown",
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
- "metadata": {},
+ "metadata": {
+ "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e"
+ },
"source": [
"# Lab | Pandas"
]
@@ -11,7 +13,9 @@
{
"cell_type": "markdown",
"id": "d1973e9e-8be6-4039-b70e-d73ee0d94c99",
- "metadata": {},
+ "metadata": {
+ "id": "d1973e9e-8be6-4039-b70e-d73ee0d94c99"
+ },
"source": [
"In this lab, we will be working with the customer data from an insurance company, which can be found in the CSV file located at the following link: https://raw.githubusercontent.com/data-bootcamp-v4/data/main/file1.csv\n",
"\n",
@@ -22,7 +26,7 @@
},
{
"cell_type": "markdown",
- "id": "8045146f-f4f7-44d9-8cd9-130d6400c73a",
+ "id": "d86a925d-21c4-4037-9fd6-a835adfb8282",
"metadata": {},
"source": [
"### Data Description\n",
@@ -47,12 +51,12 @@
"\n",
"- Vehicle Class - Type of vehicle classes that customers have Two-Door Car, Four-Door Car SUV, Luxury SUV, Sports Car, and Luxury Car\n",
"\n",
- "- Total Claim Amount - the sum of all claims made by the customer. It represents the total monetary value of all approved claims for incidents such as accidents, theft, vandalism, or other covered events.\n"
+ "- Total Claim Amount - the sum of all claims made by the customer. It represents the total monetary value of all approved claims for incidents such as accidents, theft, vandalism, or other covered events."
]
},
{
"cell_type": "markdown",
- "id": "3a72419b-20fc-4905-817a-8c83abc59de6",
+ "id": "ca63ef12-1171-4b9c-9fd9-100925d9b2fe",
"metadata": {},
"source": [
"External Resources: https://towardsdatascience.com/filtering-data-frames-in-pandas-b570b1f834b9"
@@ -61,7 +65,9 @@
{
"cell_type": "markdown",
"id": "8f8ece17-e919-4e23-96c0-c7c59778436a",
- "metadata": {},
+ "metadata": {
+ "id": "8f8ece17-e919-4e23-96c0-c7c59778436a"
+ },
"source": [
"## Challenge 1: Understanding the data\n",
"\n",
@@ -70,8 +76,10 @@
},
{
"cell_type": "markdown",
- "id": "91437bd5-59a6-49c0-8150-ef0e6e6eb253",
- "metadata": {},
+ "id": "bbf0f9a6-d7cf-4f95-9f7d-e58d2a0f0a23",
+ "metadata": {
+ "id": "bbf0f9a6-d7cf-4f95-9f7d-e58d2a0f0a23"
+ },
"source": [
"- Identify the dimensions of the dataset by determining the number of rows and columns it contains.\n",
"- Determine the data types of each column and evaluate whether they are appropriate for the nature of the variable. You should also provide suggestions for fixing any incorrect data types.\n",
@@ -82,18 +90,594 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 4,
"id": "dd4e8cd8-a6f6-486c-a5c4-1745b0c035f4",
+ "metadata": {
+ "id": "dd4e8cd8-a6f6-486c-a5c4-1745b0c035f4"
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(4008, 11)"
+ ]
+ },
+ "execution_count": 4,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "import pandas as pd\n",
+ "url = \"https://raw.githubusercontent.com/data-bootcamp-v4/data/main/file1.csv\"\n",
+ "df=pd.read_csv(url)\n",
+ "df.shape"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "id": "f34e9dde-39a6-4a22-91b1-05f26eff2bb4",
+ "metadata": {
+ "id": "f34e9dde-39a6-4a22-91b1-05f26eff2bb4"
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " customer | \n",
+ " state | \n",
+ " gender | \n",
+ " education | \n",
+ " customer_lifetime_value | \n",
+ " income | \n",
+ " monthly_premium_auto | \n",
+ " number_of_open_complaints | \n",
+ " policy_type | \n",
+ " vehicle_class | \n",
+ " total_claim_amount | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " 0 | \n",
+ " RB50392 | \n",
+ " Washington | \n",
+ " NaN | \n",
+ " Master | \n",
+ " NaN | \n",
+ " 0.0 | \n",
+ " 1000.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 2.704934 | \n",
+ "
\n",
+ " \n",
+ " 1 | \n",
+ " QZ44356 | \n",
+ " Arizona | \n",
+ " F | \n",
+ " Bachelor | \n",
+ " 697953.59% | \n",
+ " 0.0 | \n",
+ " 94.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 1131.464935 | \n",
+ "
\n",
+ " \n",
+ " 2 | \n",
+ " AI49188 | \n",
+ " Nevada | \n",
+ " F | \n",
+ " Bachelor | \n",
+ " 1288743.17% | \n",
+ " 48767.0 | \n",
+ " 108.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Two-Door Car | \n",
+ " 566.472247 | \n",
+ "
\n",
+ " \n",
+ " 3 | \n",
+ " WW63253 | \n",
+ " California | \n",
+ " M | \n",
+ " Bachelor | \n",
+ " 764586.18% | \n",
+ " 0.0 | \n",
+ " 106.0 | \n",
+ " 1/0/00 | \n",
+ " Corporate Auto | \n",
+ " SUV | \n",
+ " 529.881344 | \n",
+ "
\n",
+ " \n",
+ " 4 | \n",
+ " GA49547 | \n",
+ " Washington | \n",
+ " M | \n",
+ " High School or Below | \n",
+ " 536307.65% | \n",
+ " 36357.0 | \n",
+ " 68.0 | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ " 17.269323 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " customer state gender education customer_lifetime_value \\\n",
+ "0 RB50392 Washington NaN Master NaN \n",
+ "1 QZ44356 Arizona F Bachelor 697953.59% \n",
+ "2 AI49188 Nevada F Bachelor 1288743.17% \n",
+ "3 WW63253 California M Bachelor 764586.18% \n",
+ "4 GA49547 Washington M High School or Below 536307.65% \n",
+ "\n",
+ " income monthly_premium_auto number_of_open_complaints policy_type \\\n",
+ "0 0.0 1000.0 1/0/00 Personal Auto \n",
+ "1 0.0 94.0 1/0/00 Personal Auto \n",
+ "2 48767.0 108.0 1/0/00 Personal Auto \n",
+ "3 0.0 106.0 1/0/00 Corporate Auto \n",
+ "4 36357.0 68.0 1/0/00 Personal Auto \n",
+ "\n",
+ " vehicle_class total_claim_amount \n",
+ "0 Four-Door Car 2.704934 \n",
+ "1 Four-Door Car 1131.464935 \n",
+ "2 Two-Door Car 566.472247 \n",
+ "3 SUV 529.881344 \n",
+ "4 Four-Door Car 17.269323 "
+ ]
+ },
+ "execution_count": 6,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Not required, just for cleaner code\n",
+ "\n",
+ "# Standardize the column names\n",
+ "df.columns = df.columns.str.lower().str.replace(\" \", \"_\")\n",
+ "\n",
+ "\n",
+ "# Rename columns for clarity\n",
+ "df = df.rename(columns={\n",
+ " \"st\": \"state\"\n",
+ "})\n",
+ "\n",
+ "# Print the resulting DataFrame\n",
+ "df.head()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "id": "bf562a42-e99c-4f99-b5b0-8f433a0fabec",
+ "metadata": {
+ "id": "bf562a42-e99c-4f99-b5b0-8f433a0fabec"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Number of rows: 4008\n",
+ "Number of columns: 11\n"
+ ]
+ }
+ ],
+ "source": [
+ "# Check the number of rows and columns in the dataset\n",
+ "print(\"Number of rows: \", df.shape[0])\n",
+ "print(\"Number of columns: \", df.shape[1])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "id": "f908c12e-120d-45cc-ae6f-786be192920a",
+ "metadata": {
+ "id": "f908c12e-120d-45cc-ae6f-786be192920a"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "customer object\n",
+ "state object\n",
+ "gender object\n",
+ "education object\n",
+ "customer_lifetime_value object\n",
+ "income float64\n",
+ "monthly_premium_auto float64\n",
+ "number_of_open_complaints object\n",
+ "policy_type object\n",
+ "vehicle_class object\n",
+ "total_claim_amount float64\n",
+ "dtype: object\n"
+ ]
+ }
+ ],
+ "source": [
+ "# Explore the data types of each column in the dataset\n",
+ "print(df.dtypes)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "id": "7b48d07e-0c51-4d41-88bb-762e6d00e89c",
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "customer 1071\n",
+ "state 8\n",
+ "gender 5\n",
+ "education 6\n",
+ "customer_lifetime_value 1027\n",
+ "income 774\n",
+ "monthly_premium_auto 132\n",
+ "number_of_open_complaints 6\n",
+ "policy_type 3\n",
+ "vehicle_class 6\n",
+ "total_claim_amount 761\n",
+ "dtype: int64"
+ ]
+ },
+ "execution_count": 12,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "df.nunique() #Columns with a small number of unique values are typically categorical"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "id": "adea8fb9-572a-40e1-9819-9fea9f5931be",
+ "metadata": {
+ "id": "adea8fb9-572a-40e1-9819-9fea9f5931be"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "customer\n",
+ "1071\n",
+ "['RB50392' 'QZ44356' 'AI49188' ... 'CW49887' 'MY31220' nan]\n",
+ "state\n",
+ "8\n",
+ "['Washington' 'Arizona' 'Nevada' 'California' 'Oregon' 'Cali' 'AZ' 'WA'\n",
+ " nan]\n",
+ "gender\n",
+ "5\n",
+ "[nan 'F' 'M' 'Femal' 'Male' 'female']\n",
+ "education\n",
+ "6\n",
+ "['Master' 'Bachelor' 'High School or Below' 'College' 'Bachelors' 'Doctor'\n",
+ " nan]\n",
+ "customer_lifetime_value\n",
+ "1027\n",
+ "[nan '697953.59%' '1288743.17%' ... '2031499.76%' '323912.47%'\n",
+ " '899704.02%']\n",
+ "income\n",
+ "774\n",
+ "0.0 99960.0\n",
+ "monthly_premium_auto\n",
+ "132\n",
+ "61.0 35354.0\n",
+ "number_of_open_complaints\n",
+ "6\n",
+ "['1/0/00' '1/2/00' '1/1/00' '1/3/00' '1/5/00' '1/4/00' nan]\n",
+ "policy_type\n",
+ "3\n",
+ "['Personal Auto' 'Corporate Auto' 'Special Auto' nan]\n",
+ "vehicle_class\n",
+ "6\n",
+ "['Four-Door Car' 'Two-Door Car' 'SUV' 'Luxury SUV' 'Sports Car'\n",
+ " 'Luxury Car' nan]\n",
+ "total_claim_amount\n",
+ "761\n",
+ "0.382107 2893.239678\n"
+ ]
+ }
+ ],
+ "source": [
+ "# Examine the unique values of categorical columns and the range of values for numerical columns\n",
+ "for col in df.columns:\n",
+ " if df[col].dtype == 'object':\n",
+ " print(col)\n",
+ " print(df[col].nunique())\n",
+ " print(df[col].unique())\n",
+ " else:\n",
+ " print(col)\n",
+ " print(df[col].nunique())\n",
+ " print(df[col].min(), df[col].max())"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "id": "12ba0ecf-516e-4631-a3af-1f5386ba5cd5",
+ "metadata": {
+ "id": "12ba0ecf-516e-4631-a3af-1f5386ba5cd5"
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " income | \n",
+ " monthly_premium_auto | \n",
+ " total_claim_amount | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " count | \n",
+ " 1071.000000 | \n",
+ " 1071.000000 | \n",
+ " 1071.000000 | \n",
+ "
\n",
+ " \n",
+ " mean | \n",
+ " 39295.701214 | \n",
+ " 193.234360 | \n",
+ " 404.986909 | \n",
+ "
\n",
+ " \n",
+ " std | \n",
+ " 30469.427060 | \n",
+ " 1601.190369 | \n",
+ " 293.027260 | \n",
+ "
\n",
+ " \n",
+ " min | \n",
+ " 0.000000 | \n",
+ " 61.000000 | \n",
+ " 0.382107 | \n",
+ "
\n",
+ " \n",
+ " 25% | \n",
+ " 14072.000000 | \n",
+ " 68.000000 | \n",
+ " 202.157702 | \n",
+ "
\n",
+ " \n",
+ " 50% | \n",
+ " 36234.000000 | \n",
+ " 83.000000 | \n",
+ " 354.729129 | \n",
+ "
\n",
+ " \n",
+ " 75% | \n",
+ " 64631.000000 | \n",
+ " 109.500000 | \n",
+ " 532.800000 | \n",
+ "
\n",
+ " \n",
+ " max | \n",
+ " 99960.000000 | \n",
+ " 35354.000000 | \n",
+ " 2893.239678 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " income monthly_premium_auto total_claim_amount\n",
+ "count 1071.000000 1071.000000 1071.000000\n",
+ "mean 39295.701214 193.234360 404.986909\n",
+ "std 30469.427060 1601.190369 293.027260\n",
+ "min 0.000000 61.000000 0.382107\n",
+ "25% 14072.000000 68.000000 202.157702\n",
+ "50% 36234.000000 83.000000 354.729129\n",
+ "75% 64631.000000 109.500000 532.800000\n",
+ "max 99960.000000 35354.000000 2893.239678"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
"source": [
- "# Your code here"
+ "# Compute summary statistics of the dataset such as mean, median, mode, standard deviation, and quartiles to understand the central tendency and distribution of the data\n",
+ "display(df.describe(include='number'))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 16,
+ "id": "8ebd3b6d-c82f-4503-a520-d10933ecfd1b",
+ "metadata": {
+ "id": "8ebd3b6d-c82f-4503-a520-d10933ecfd1b"
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " customer | \n",
+ " state | \n",
+ " gender | \n",
+ " education | \n",
+ " customer_lifetime_value | \n",
+ " number_of_open_complaints | \n",
+ " policy_type | \n",
+ " vehicle_class | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " count | \n",
+ " 1071 | \n",
+ " 1071 | \n",
+ " 954 | \n",
+ " 1071 | \n",
+ " 1068 | \n",
+ " 1071 | \n",
+ " 1071 | \n",
+ " 1071 | \n",
+ "
\n",
+ " \n",
+ " unique | \n",
+ " 1071 | \n",
+ " 8 | \n",
+ " 5 | \n",
+ " 6 | \n",
+ " 1027 | \n",
+ " 6 | \n",
+ " 3 | \n",
+ " 6 | \n",
+ "
\n",
+ " \n",
+ " top | \n",
+ " RB50392 | \n",
+ " Oregon | \n",
+ " F | \n",
+ " Bachelor | \n",
+ " 445811.34% | \n",
+ " 1/0/00 | \n",
+ " Personal Auto | \n",
+ " Four-Door Car | \n",
+ "
\n",
+ " \n",
+ " freq | \n",
+ " 1 | \n",
+ " 320 | \n",
+ " 457 | \n",
+ " 324 | \n",
+ " 4 | \n",
+ " 830 | \n",
+ " 780 | \n",
+ " 576 | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ " customer state gender education customer_lifetime_value \\\n",
+ "count 1071 1071 954 1071 1068 \n",
+ "unique 1071 8 5 6 1027 \n",
+ "top RB50392 Oregon F Bachelor 445811.34% \n",
+ "freq 1 320 457 324 4 \n",
+ "\n",
+ " number_of_open_complaints policy_type vehicle_class \n",
+ "count 1071 1071 1071 \n",
+ "unique 6 3 6 \n",
+ "top 1/0/00 Personal Auto Four-Door Car \n",
+ "freq 830 780 576 "
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "display(df.describe(include='object'))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 18,
+ "id": "4e74fdf2-f62f-4bca-aeb3-83a42440c4c9",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "gender\n",
+ "F 457\n",
+ "M 413\n",
+ "Male 39\n",
+ "female 28\n",
+ "Femal 17\n",
+ "Name: count, dtype: int64"
+ ]
+ },
+ "execution_count": 18,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "df.gender.value_counts()"
]
},
{
"cell_type": "markdown",
"id": "4a703890-63db-4944-b7ab-95a4f8185120",
- "metadata": {},
+ "metadata": {
+ "id": "4a703890-63db-4944-b7ab-95a4f8185120"
+ },
"source": [
"## Challenge 2: analyzing the data"
]
@@ -101,7 +685,9 @@
{
"cell_type": "markdown",
"id": "0776a403-c56a-452f-ac33-5fd4fdb06fc7",
- "metadata": {},
+ "metadata": {
+ "id": "0776a403-c56a-452f-ac33-5fd4fdb06fc7"
+ },
"source": [
"### Exercise 1"
]
@@ -109,25 +695,62 @@
{
"cell_type": "markdown",
"id": "eedbc484-da4d-4f9c-9343-e1d44311a87e",
- "metadata": {},
+ "metadata": {
+ "id": "eedbc484-da4d-4f9c-9343-e1d44311a87e"
+ },
"source": [
"The marketing team wants to know the top 5 less common customer locations. Create a pandas Series object that contains the customer locations and their frequencies, and then retrieve the top 5 less common locations in ascending order."
]
},
+ {
+ "cell_type": "markdown",
+ "id": "a4889ecb-5da5-4c97-a347-ca00fbb2b0ae",
+ "metadata": {
+ "id": "a4889ecb-5da5-4c97-a347-ca00fbb2b0ae"
+ },
+ "source": [
+ "*Hint:*\n",
+ "- *Using value_counts() method simplifies this analysis.*"
+ ]
+ },
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 20,
"id": "2dca5073-4520-4f42-9390-4b92733284ed",
- "metadata": {},
- "outputs": [],
+ "metadata": {
+ "id": "2dca5073-4520-4f42-9390-4b92733284ed"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "state\n",
+ "AZ 25\n",
+ "WA 30\n",
+ "Washington 81\n",
+ "Nevada 98\n",
+ "Cali 120\n",
+ "Name: count, dtype: int64\n"
+ ]
+ }
+ ],
"source": [
- "# Your code here"
+ "# Create a Series with the customer locations and their frequencies\n",
+ "locations = df['state'].value_counts()\n",
+ "\n",
+ "# Retrieve the top 5 most common locations\n",
+ "top_locations = locations.tail(5).sort_values()\n",
+ "\n",
+ "print(top_locations)"
]
},
{
"cell_type": "markdown",
- "id": "0ce80f43-4afa-43c7-a78a-c917444da4e0",
- "metadata": {},
+ "id": "82652369-bba8-447c-9ef2-eda16c671452",
+ "metadata": {
+ "id": "82652369-bba8-447c-9ef2-eda16c671452"
+ },
"source": [
"### Exercise 2\n",
"\n",
@@ -136,8 +759,10 @@
},
{
"cell_type": "markdown",
- "id": "a9f13997-1555-4f98-aca6-970fda1d2c3f",
- "metadata": {},
+ "id": "bd770f57-0061-4b2c-b3ed-b448c9772feb",
+ "metadata": {
+ "id": "bd770f57-0061-4b2c-b3ed-b448c9772feb"
+ },
"source": [
"*Hint:*\n",
"- *Using value_counts() method simplifies this analysis.*\n",
@@ -146,56 +771,110 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 22,
"id": "bcfad6c1-9af2-4b0b-9aa9-0dc5c17473c0",
- "metadata": {},
- "outputs": [],
+ "metadata": {
+ "id": "bcfad6c1-9af2-4b0b-9aa9-0dc5c17473c0"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The policy type with the highest number of policies sold is Personal Auto\n"
+ ]
+ }
+ ],
"source": [
- "# Your code here"
+ "# Create a pandas Series object with the policy types and their total number of policies sold\n",
+ "policy_counts = df[\"policy_type\"].value_counts()\n",
+ "\n",
+ "# Retrieve the policy type with the highest number of policies sold\n",
+ "top_policy_type = policy_counts.idxmax()\n",
+ "\n",
+ "print(f\"The policy type with the highest number of policies sold is {top_policy_type}\")\n"
]
},
{
"cell_type": "markdown",
- "id": "0b863fd3-bf91-4d5d-86eb-be29ed9f5b70",
- "metadata": {},
+ "id": "5e5312b2-77a2-4f9a-a050-dadf54dfb0f7",
+ "metadata": {
+ "id": "5e5312b2-77a2-4f9a-a050-dadf54dfb0f7"
+ },
"source": [
"### Exercise 3\n",
"\n",
- "The sales team wants to know if customers with Personal Auto have a lower income than those with Corporate Auto. How does the average income compare between the two policy types?"
+ "The sales team wants to know if customers with Personal Auto have a income than those with Corporate Auto. How does the average income compare between the two policy types?"
]
},
{
"cell_type": "markdown",
- "id": "b1386d75-2810-4aa1-93e0-9485aa12d552",
- "metadata": {},
+ "id": "579590ea-2e79-4027-91b7-06fda83b22dc",
+ "metadata": {
+ "id": "579590ea-2e79-4027-91b7-06fda83b22dc"
+ },
"source": [
- "- Use *loc* to create two dataframes: one containing only Personal Auto policies and one containing only Corporate Auto policies.\n",
+ "- Use loc to create two dataframes: one containing only Personal Auto policies and one containing only Corporate Auto policies.\n",
"- Calculate the average income for each policy.\n",
"- Print the results."
]
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 24,
"id": "0c0563cf-6f8b-463d-a321-651a972f82e5",
- "metadata": {},
+ "metadata": {
+ "id": "0c0563cf-6f8b-463d-a321-651a972f82e5"
+ },
"outputs": [],
"source": [
- "# Your code here"
+ "# Use loc to select rows with policy type 'Personal Auto' and 'Corporate Auto'\n",
+ "personal_auto_df = df.loc[df['policy_type'] == 'Personal Auto']\n",
+ "corporate_auto_df = df.loc[df['policy_type'] == 'Corporate Auto']"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 26,
+ "id": "cedc86a8-3e88-40a8-a626-acd93910875f",
+ "metadata": {
+ "id": "cedc86a8-3e88-40a8-a626-acd93910875f"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Average income for Personal Auto policies: 38180.69871794872\n",
+ "Average income for Corporate Auto policies: 41390.31196581197\n"
+ ]
+ }
+ ],
+ "source": [
+ "# Calculate the mean for each policy type\n",
+ "personal_auto_avg_clv = personal_auto_df['income'].mean()\n",
+ "corporate_auto_avg_clv = corporate_auto_df['income'].mean()\n",
+ "\n",
+ "print(\"Average income for Personal Auto policies:\", personal_auto_avg_clv)\n",
+ "print(\"Average income for Corporate Auto policies:\", corporate_auto_avg_clv)"
]
},
{
"cell_type": "markdown",
- "id": "80b16c27-f4a5-4727-a229-1f88671cf4e2",
- "metadata": {},
+ "id": "23cf226c-7111-43f4-ada3-54b5d00baaad",
+ "metadata": {
+ "id": "23cf226c-7111-43f4-ada3-54b5d00baaad"
+ },
"source": [
"### Bonus: Exercise 4\n"
]
},
{
"cell_type": "markdown",
- "id": "ac584986-299b-475f-ac2e-928c16c3f512",
- "metadata": {},
+ "id": "136e10bc-091f-46f6-9120-4a35bc6f0d4a",
+ "metadata": {
+ "id": "136e10bc-091f-46f6-9120-4a35bc6f0d4a"
+ },
"source": [
"Your goal is to identify customers with a high policy claim amount.\n",
"\n",
@@ -203,13 +882,15 @@
"\n",
"- Review again the statistics for total claim amount to gain an understanding of the data.\n",
"- To identify potential areas for improving customer retention and profitability, we want to focus on customers with a high policy claim amount. Consider customers with a high policy claim amount to be those in the top 25% of the total claim amount. Create a pandas DataFrame object that contains information about customers with a policy claim amount greater than the 75th percentile.\n",
- "- Use DataFrame methods to calculate summary statistics about the high policy claim amount data. "
+ "- Use DataFrame methods to calculate summary statistics about the high policy claim amount data."
]
},
{
"cell_type": "markdown",
- "id": "4e3af5f1-6023-4b05-9c01-d05392daa650",
- "metadata": {},
+ "id": "d619362a-f9c5-491b-83bb-b48277a8c3ad",
+ "metadata": {
+ "id": "d619362a-f9c5-491b-83bb-b48277a8c3ad"
+ },
"source": [
"*Note: When analyzing data, we often want to focus on certain groups of values to gain insights. Percentiles are a useful tool to help us define these groups. A percentile is a measure that tells us what percentage of values in a dataset are below a certain value. For example, the 75th percentile represents the value below which 75% of the data falls. Similarly, the 25th percentile represents the value below which 25% of the data falls. When we talk about the top 25%, we are referring to the values that fall above the 75th percentile, which represent the top quarter of the data. On the other hand, when we talk about the bottom 25%, we are referring to the values that fall below the 25th percentile, which represent the bottom quarter of the data. By focusing on these groups, we can identify patterns and trends that may be useful for making decisions and taking action.*\n",
"\n",
@@ -217,25 +898,90 @@
]
},
{
- "cell_type": "markdown",
- "id": "2d234634-50bd-41e0-88f7-d5ba684455d1",
- "metadata": {},
+ "cell_type": "code",
+ "execution_count": 28,
+ "id": "b731bca6-a760-4860-a27b-a33efa712ce0",
+ "metadata": {
+ "id": "b731bca6-a760-4860-a27b-a33efa712ce0"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Total Claim Amount Stats:\n",
+ " count 1071.000000\n",
+ "mean 404.986909\n",
+ "std 293.027260\n",
+ "min 0.382107\n",
+ "25% 202.157702\n",
+ "50% 354.729129\n",
+ "75% 532.800000\n",
+ "max 2893.239678\n",
+ "Name: total_claim_amount, dtype: float64\n",
+ "\n",
+ "Income Stats:\n",
+ " count 1071.000000\n",
+ "mean 39295.701214\n",
+ "std 30469.427060\n",
+ "min 0.000000\n",
+ "25% 14072.000000\n",
+ "50% 36234.000000\n",
+ "75% 64631.000000\n",
+ "max 99960.000000\n",
+ "Name: income, dtype: float64\n"
+ ]
+ }
+ ],
"source": [
- "*Hint 2: check `Boolean selection according to the values of a single column` in https://towardsdatascience.com/filtering-data-frames-in-pandas-b570b1f834b9*"
+ "# Look at statistics for total claim amount and customer lifetime value\n",
+ "print(\"Total Claim Amount Stats:\\n\", df[\"total_claim_amount\"].describe())\n",
+ "print(\"\\nIncome Stats:\\n\", df[\"income\"].describe())"
]
},
{
"cell_type": "code",
- "execution_count": null,
- "id": "b731bca6-a760-4860-a27b-a33efa712ce0",
- "metadata": {},
- "outputs": [],
+ "execution_count": 30,
+ "id": "a20e16a0-cd67-44ff-b8b9-0edefa8b3eff",
+ "metadata": {
+ "id": "a20e16a0-cd67-44ff-b8b9-0edefa8b3eff"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\n",
+ "Summary Statistics for High Claim Amount and Low Income Customers:\n",
+ " total_claim_amount\n",
+ "count 264.000000\n",
+ "mean 782.228263\n",
+ "std 292.751640\n",
+ "min 537.600000\n",
+ "25% 606.521741\n",
+ "50% 679.597985\n",
+ "75% 851.400000\n",
+ "max 2893.239678\n"
+ ]
+ }
+ ],
"source": [
- "# Your code here"
+ "# Define threshold values for high claim amount and low customer lifetime value\n",
+ "high_claim_amount = df[\"total_claim_amount\"].quantile(0.75)\n",
+ "\n",
+ "# Filter the data to identify customers with high claim amount\n",
+ "df_high = df[(df[\"total_claim_amount\"] > high_claim_amount)]\n",
+ "\n",
+ "# Calculate summary statistics for the filtered data\n",
+ "print(\"\\nSummary Statistics for High Claim Amount and Low Income Customers:\")\n",
+ "print(df_high[[\"total_claim_amount\"]].describe())"
]
}
],
"metadata": {
+ "colab": {
+ "provenance": []
+ },
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
@@ -251,7 +997,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.9.13"
+ "version": "3.12.4"
}
},
"nbformat": 4,