From 13f4bf930f84e050c6500c0ab0df31e1c6e59fcf Mon Sep 17 00:00:00 2001 From: Teresa Mendes Coelho Date: Sun, 28 Jun 2026 13:25:28 +0100 Subject: [PATCH] Solved lab --- lab-dw-data-structuring-and-combining.ipynb | 1508 ++++++++++++++++++- 1 file changed, 1498 insertions(+), 10 deletions(-) diff --git a/lab-dw-data-structuring-and-combining.ipynb b/lab-dw-data-structuring-and-combining.ipynb index ec4e3f9..c83bb68 100644 --- a/lab-dw-data-structuring-and-combining.ipynb +++ b/lab-dw-data-structuring-and-combining.ipynb @@ -36,14 +36,442 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "492d06e3-92c7-4105-ac72-536db98d3244", "metadata": { "id": "492d06e3-92c7-4105-ac72-536db98d3244" }, "outputs": [], "source": [ - "# Your code goes here" + "import pandas as pd\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "c7a21e8c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.0.3\n" + ] + } + ], + "source": [ + "import pandas as pd\n", + "\n", + "print(pd.__version__)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "6760a859", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.microsoft.datawrangler.viewer.v0+json": { + "columns": [ + { + "name": "index", + "rawType": "int64", + "type": "integer" + }, + { + "name": "Customer", + "rawType": "str", + "type": "string" + }, + { + "name": "ST", + "rawType": "str", + "type": "string" + }, + { + "name": "GENDER", + "rawType": "str", + "type": "string" + }, + { + "name": "Education", + "rawType": "str", + "type": "string" + }, + { + "name": "Customer Lifetime Value", + "rawType": "object", + "type": "unknown" + }, + { + "name": "Income", + "rawType": "float64", + "type": "float" + }, + { + "name": "Monthly Premium Auto", + "rawType": "float64", + "type": "float" + }, + { + "name": "Number of Open Complaints", + "rawType": "object", + "type": "string" + }, + { + "name": "Policy Type", + "rawType": "str", + "type": "string" + }, + { + "name": "Vehicle Class", + "rawType": "str", + "type": "string" + }, + { + "name": "Total Claim Amount", + "rawType": "float64", + "type": "float" + }, + { + "name": "State", + "rawType": "str", + "type": "string" + }, + { + "name": "Gender", + "rawType": "str", + "type": "string" + } + ], + "ref": "0d458bf9-8e6b-4690-83a3-3d0294210270", + "rows": [ + [ + "0", + "RB50392", + "Washington", + null, + "Master", + null, + "0.0", + "1000.0", + "1/0/00", + "Personal Auto", + "Four-Door Car", + "2.704934", + null, + null + ], + [ + "1", + "QZ44356", + "Arizona", + "F", + "Bachelor", + "697953.59%", + "0.0", + "94.0", + "1/0/00", + "Personal Auto", + "Four-Door Car", + "1131.464935", + null, + null + ], + [ + "2", + "AI49188", + "Nevada", + "F", + "Bachelor", + "1288743.17%", + "48767.0", + "108.0", + "1/0/00", + "Personal Auto", + "Two-Door Car", + "566.472247", + null, + null + ], + [ + "3", + "WW63253", + "California", + "M", + "Bachelor", + "764586.18%", + "0.0", + "106.0", + "1/0/00", + "Corporate Auto", + "SUV", + "529.881344", + null, + null + ], + [ + "4", + "GA49547", + "Washington", + "M", + "High School or Below", + "536307.65%", + "36357.0", + "68.0", + "1/0/00", + "Personal Auto", + "Four-Door Car", + "17.269323", + null, + null + ] + ], + "shape": { + "columns": 13, + "rows": 5 + } + }, + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
CustomerSTGENDEREducationCustomer Lifetime ValueIncomeMonthly Premium AutoNumber of Open ComplaintsPolicy TypeVehicle ClassTotal Claim AmountStateGender
0RB50392WashingtonNaNMasterNaN0.01000.01/0/00Personal AutoFour-Door Car2.704934NaNNaN
1QZ44356ArizonaFBachelor697953.59%0.094.01/0/00Personal AutoFour-Door Car1131.464935NaNNaN
2AI49188NevadaFBachelor1288743.17%48767.0108.01/0/00Personal AutoTwo-Door Car566.472247NaNNaN
3WW63253CaliforniaMBachelor764586.18%0.0106.01/0/00Corporate AutoSUV529.881344NaNNaN
4GA49547WashingtonMHigh School or Below536307.65%36357.068.01/0/00Personal AutoFour-Door Car17.269323NaNNaN
\n", + "
" + ], + "text/plain": [ + " Customer ST 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 State Gender \n", + "0 Four-Door Car 2.704934 NaN NaN \n", + "1 Four-Door Car 1131.464935 NaN NaN \n", + "2 Two-Door Car 566.472247 NaN NaN \n", + "3 SUV 529.881344 NaN NaN \n", + "4 Four-Door Car 17.269323 NaN NaN " + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import pandas as pd\n", + "\n", + "# Load the three datasets\n", + "file1 = pd.read_csv(\"https://raw.githubusercontent.com/data-bootcamp-v4/data/main/file1.csv\")\n", + "file2 = pd.read_csv(\"https://raw.githubusercontent.com/data-bootcamp-v4/data/main/file2.csv\")\n", + "file3 = pd.read_csv(\"https://raw.githubusercontent.com/data-bootcamp-v4/data/main/file3.csv\")\n", + "\n", + "# Combine them into one DataFrame\n", + "df = pd.concat([file1, file2, file3], ignore_index=True)\n", + "\n", + "# Display the first rows\n", + "df.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "44146957", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "RangeIndex: 12074 entries, 0 to 12073\n", + "Data columns (total 13 columns):\n", + " # Column Non-Null Count Dtype \n", + "--- ------ -------------- ----- \n", + " 0 Customer 9137 non-null str \n", + " 1 ST 2067 non-null str \n", + " 2 GENDER 1945 non-null str \n", + " 3 Education 9137 non-null str \n", + " 4 Customer Lifetime Value 9130 non-null object \n", + " 5 Income 9137 non-null float64\n", + " 6 Monthly Premium Auto 9137 non-null float64\n", + " 7 Number of Open Complaints 9137 non-null object \n", + " 8 Policy Type 9137 non-null str \n", + " 9 Vehicle Class 9137 non-null str \n", + " 10 Total Claim Amount 9137 non-null float64\n", + " 11 State 7070 non-null str \n", + " 12 Gender 7070 non-null str \n", + "dtypes: float64(3), object(2), str(8)\n", + "memory usage: 1.2+ MB\n" + ] + } + ], + "source": [ + "# Check the shape of the DataFrame\n", + "df.shape\n", + "\n", + "# Display the column names\n", + "df.columns\n", + "\n", + "# Display information about the DataFrame\n", + "df.info()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "09999c9b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Index(['customer', 'st', 'gender', 'education', 'customer_lifetime_value',\n", + " 'income', 'monthly_premium_auto', 'number_of_open_complaints',\n", + " 'policy_type', 'vehicle_class', 'total_claim_amount', 'state',\n", + " 'gender'],\n", + " dtype='str')" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Standardize column names\n", + "df.columns = (\n", + " df.columns\n", + " .str.lower()\n", + " .str.strip()\n", + " .str.replace(\" \", \"_\")\n", + ")\n", + "\n", + "df.columns" ] }, { @@ -72,14 +500,574 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "id": "aa10d9b0-1c27-4d3f-a8e4-db6ab73bfd26", "metadata": { "id": "aa10d9b0-1c27-4d3f-a8e4-db6ab73bfd26" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "RangeIndex: 12074 entries, 0 to 12073\n", + "Data columns (total 13 columns):\n", + " # Column Non-Null Count Dtype \n", + "--- ------ -------------- ----- \n", + " 0 customer 9137 non-null str \n", + " 1 st 2067 non-null str \n", + " 2 gender 1945 non-null str \n", + " 3 education 9137 non-null str \n", + " 4 customer_lifetime_value 9130 non-null object \n", + " 5 income 9137 non-null float64\n", + " 6 monthly_premium_auto 9137 non-null float64\n", + " 7 number_of_open_complaints 9137 non-null object \n", + " 8 policy_type 9137 non-null str \n", + " 9 vehicle_class 9137 non-null str \n", + " 10 total_claim_amount 9137 non-null float64\n", + " 11 state 7070 non-null str \n", + " 12 gender 7070 non-null str \n", + "dtypes: float64(3), object(2), str(8)\n", + "memory usage: 1.2+ MB\n" + ] + } + ], + "source": [ + "df.shape\n", + "df.columns\n", + "df.info()" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "0e0bce20", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.microsoft.datawrangler.viewer.v0+json": { + "columns": [ + { + "name": "index", + "rawType": "int64", + "type": "integer" + }, + { + "name": "unnamed:_0", + "rawType": "int64", + "type": "integer" + }, + { + "name": "customer", + "rawType": "str", + "type": "string" + }, + { + "name": "state", + "rawType": "str", + "type": "string" + }, + { + "name": "customer_lifetime_value", + "rawType": "float64", + "type": "float" + }, + { + "name": "response", + "rawType": "str", + "type": "string" + }, + { + "name": "coverage", + "rawType": "str", + "type": "string" + }, + { + "name": "education", + "rawType": "str", + "type": "string" + }, + { + "name": "effective_to_date", + "rawType": "str", + "type": "string" + }, + { + "name": "employmentstatus", + "rawType": "str", + "type": "string" + }, + { + "name": "gender", + "rawType": "str", + "type": "string" + }, + { + "name": "income", + "rawType": "int64", + "type": "integer" + }, + { + "name": "location_code", + "rawType": "str", + "type": "string" + }, + { + "name": "marital_status", + "rawType": "str", + "type": "string" + }, + { + "name": "monthly_premium_auto", + "rawType": "int64", + "type": "integer" + }, + { + "name": "months_since_last_claim", + "rawType": "float64", + "type": "float" + }, + { + "name": "months_since_policy_inception", + "rawType": "int64", + "type": "integer" + }, + { + "name": "number_of_open_complaints", + "rawType": "float64", + "type": "float" + }, + { + "name": "number_of_policies", + "rawType": "int64", + "type": "integer" + }, + { + "name": "policy_type", + "rawType": "str", + "type": "string" + }, + { + "name": "policy", + "rawType": "str", + "type": "string" + }, + { + "name": "renew_offer_type", + "rawType": "str", + "type": "string" + }, + { + "name": "sales_channel", + "rawType": "str", + "type": "string" + }, + { + "name": "total_claim_amount", + "rawType": "float64", + "type": "float" + }, + { + "name": "vehicle_class", + "rawType": "str", + "type": "string" + }, + { + "name": "vehicle_size", + "rawType": "str", + "type": "string" + }, + { + "name": "vehicle_type", + "rawType": "str", + "type": "string" + }, + { + "name": "month", + "rawType": "int64", + "type": "integer" + } + ], + "ref": "eaf71d69-e3cb-40ce-aa49-a034f5dc140a", + "rows": [ + [ + "0", + "0", + "DK49336", + "Arizona", + "4809.21696", + "No", + "Basic", + "College", + "2011-02-18", + "Employed", + "M", + "48029", + "Suburban", + "Married", + "61", + "7.0", + "52", + "0.0", + "9", + "Corporate Auto", + "Corporate L3", + "Offer3", + "Agent", + "292.8", + "Four-Door Car", + "Medsize", + "A", + "2" + ], + [ + "1", + "1", + "KX64629", + "California", + "2228.525238", + "No", + "Basic", + "College", + "2011-01-18", + "Unemployed", + "F", + "0", + "Suburban", + "Single", + "64", + "3.0", + "26", + "0.0", + "1", + "Personal Auto", + "Personal L3", + "Offer4", + "Call Center", + "744.924331", + "Four-Door Car", + "Medsize", + "A", + "1" + ], + [ + "2", + "2", + "LZ68649", + "Washington", + "14947.9173", + "No", + "Basic", + "Bachelor", + "2011-02-10", + "Employed", + "M", + "22139", + "Suburban", + "Single", + "100", + "34.0", + "31", + "0.0", + "2", + "Personal Auto", + "Personal L3", + "Offer3", + "Call Center", + "480.0", + "SUV", + "Medsize", + "A", + "2" + ], + [ + "3", + "3", + "XL78013", + "Oregon", + "22332.43946", + "Yes", + "Extended", + "College", + "2011-01-11", + "Employed", + "M", + "49078", + "Suburban", + "Single", + "97", + "10.0", + "3", + "0.0", + "2", + "Corporate Auto", + "Corporate L3", + "Offer2", + "Branch", + "484.013411", + "Four-Door Car", + "Medsize", + "A", + "1" + ], + [ + "4", + "4", + "QA50777", + "Oregon", + "9025.067525", + "No", + "Premium", + "Bachelor", + "2011-01-17", + "Medical Leave", + "F", + "23675", + "Suburban", + "Married", + "117", + "15.149070740488469", + "31", + "0.384256105867471", + "7", + "Personal Auto", + "Personal L2", + "Offer1", + "Branch", + "707.925645", + "Four-Door Car", + "Medsize", + "A", + "1" + ] + ], + "shape": { + "columns": 27, + "rows": 5 + } + }, + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
unnamed:_0customerstatecustomer_lifetime_valueresponsecoverageeducationeffective_to_dateemploymentstatusgender...number_of_policiespolicy_typepolicyrenew_offer_typesales_channeltotal_claim_amountvehicle_classvehicle_sizevehicle_typemonth
00DK49336Arizona4809.216960NoBasicCollege2011-02-18EmployedM...9Corporate AutoCorporate L3Offer3Agent292.800000Four-Door CarMedsizeA2
11KX64629California2228.525238NoBasicCollege2011-01-18UnemployedF...1Personal AutoPersonal L3Offer4Call Center744.924331Four-Door CarMedsizeA1
22LZ68649Washington14947.917300NoBasicBachelor2011-02-10EmployedM...2Personal AutoPersonal L3Offer3Call Center480.000000SUVMedsizeA2
33XL78013Oregon22332.439460YesExtendedCollege2011-01-11EmployedM...2Corporate AutoCorporate L3Offer2Branch484.013411Four-Door CarMedsizeA1
44QA50777Oregon9025.067525NoPremiumBachelor2011-01-17Medical LeaveF...7Personal AutoPersonal L2Offer1Branch707.925645Four-Door CarMedsizeA1
\n", + "

5 rows × 27 columns

\n", + "
" + ], + "text/plain": [ + " unnamed:_0 customer state customer_lifetime_value response \\\n", + "0 0 DK49336 Arizona 4809.216960 No \n", + "1 1 KX64629 California 2228.525238 No \n", + "2 2 LZ68649 Washington 14947.917300 No \n", + "3 3 XL78013 Oregon 22332.439460 Yes \n", + "4 4 QA50777 Oregon 9025.067525 No \n", + "\n", + " coverage education effective_to_date employmentstatus gender ... \\\n", + "0 Basic College 2011-02-18 Employed M ... \n", + "1 Basic College 2011-01-18 Unemployed F ... \n", + "2 Basic Bachelor 2011-02-10 Employed M ... \n", + "3 Extended College 2011-01-11 Employed M ... \n", + "4 Premium Bachelor 2011-01-17 Medical Leave F ... \n", + "\n", + " number_of_policies policy_type policy renew_offer_type \\\n", + "0 9 Corporate Auto Corporate L3 Offer3 \n", + "1 1 Personal Auto Personal L3 Offer4 \n", + "2 2 Personal Auto Personal L3 Offer3 \n", + "3 2 Corporate Auto Corporate L3 Offer2 \n", + "4 7 Personal Auto Personal L2 Offer1 \n", + "\n", + " sales_channel total_claim_amount vehicle_class vehicle_size \\\n", + "0 Agent 292.800000 Four-Door Car Medsize \n", + "1 Call Center 744.924331 Four-Door Car Medsize \n", + "2 Call Center 480.000000 SUV Medsize \n", + "3 Branch 484.013411 Four-Door Car Medsize \n", + "4 Branch 707.925645 Four-Door Car Medsize \n", + "\n", + " vehicle_type month \n", + "0 A 2 \n", + "1 A 1 \n", + "2 A 2 \n", + "3 A 1 \n", + "4 A 1 \n", + "\n", + "[5 rows x 27 columns]" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code goes here" + "import pandas as pd\n", + "\n", + "marketing = pd.read_csv(\n", + " \"https://raw.githubusercontent.com/data-bootcamp-v4/data/main/marketing_customer_analysis_clean.csv\"\n", + ")\n", + "\n", + "marketing.head()" ] }, { @@ -93,6 +1081,200 @@ "Round the total revenue to 2 decimal points. Analyze the resulting table to draw insights." ] }, + { + "cell_type": "code", + "execution_count": 22, + "id": "ed82f6e5", + "metadata": {}, + "outputs": [ + { + "ename": "KeyError", + "evalue": "'Total Claim Amount'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mKeyError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[22]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m sales_channel_revenue = marketing.pivot_table(\n\u001b[32m 2\u001b[39m index=\u001b[33m\"Sales Channel\"\u001b[39m,\n\u001b[32m 3\u001b[39m values=\u001b[33m\"Total Claim Amount\"\u001b[39m,\n\u001b[32m 4\u001b[39m aggfunc=\u001b[33m\"sum\"\u001b[39m\n", + "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\teres\\AppData\\Local\\Programs\\Python\\Python314\\Lib\\site-packages\\pandas\\core\\frame.py:11159\u001b[39m, in \u001b[36mDataFrame.pivot_table\u001b[39m\u001b[34m(self, values, index, columns, aggfunc, fill_value, margins, dropna, margins_name, observed, sort, **kwargs)\u001b[39m\n\u001b[32m 11155\u001b[39m **kwargs,\n\u001b[32m 11156\u001b[39m ) -> DataFrame:\n\u001b[32m 11157\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m pandas.core.reshape.pivot \u001b[38;5;28;01mimport\u001b[39;00m pivot_table\n\u001b[32m 11158\u001b[39m \n\u001b[32m> \u001b[39m\u001b[32m11159\u001b[39m return pivot_table(\n\u001b[32m 11160\u001b[39m self,\n\u001b[32m 11161\u001b[39m values=values,\n\u001b[32m 11162\u001b[39m index=index,\n", + "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\teres\\AppData\\Local\\Programs\\Python\\Python314\\Lib\\site-packages\\pandas\\core\\reshape\\pivot.py:267\u001b[39m, in \u001b[36mpivot_table\u001b[39m\u001b[34m(data, values, index, columns, aggfunc, fill_value, margins, dropna, margins_name, observed, sort, **kwargs)\u001b[39m\n\u001b[32m 264\u001b[39m table = concat(pieces, keys=keys, axis=\u001b[32m1\u001b[39m)\n\u001b[32m 265\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m table.__finalize__(data, method=\u001b[33m\"\u001b[39m\u001b[33mpivot_table\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m--> \u001b[39m\u001b[32m267\u001b[39m table = \u001b[30;43m__internal_pivot_table\u001b[39;49m\u001b[30;43m(\u001b[39;49m\n\u001b[32m 268\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mdata\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 269\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mvalues\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 270\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mindex\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 271\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mcolumns\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 272\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43maggfunc\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 273\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mfill_value\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 274\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mmargins\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 275\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mdropna\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 276\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mmargins_name\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 277\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mobserved\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 278\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43msort\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 279\u001b[39m \u001b[30;43m \u001b[39;49m\u001b[30;43mkwargs\u001b[39;49m\u001b[30;43m,\u001b[39;49m\n\u001b[32m 280\u001b[39m \u001b[30;43m\u001b[39;49m\u001b[30;43m)\u001b[39;49m\n\u001b[32m 281\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m table.__finalize__(data, method=\u001b[33m\"\u001b[39m\u001b[33mpivot_table\u001b[39m\u001b[33m\"\u001b[39m)\n", + "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\teres\\AppData\\Local\\Programs\\Python\\Python314\\Lib\\site-packages\\pandas\\core\\reshape\\pivot.py:315\u001b[39m, in \u001b[36m__internal_pivot_table\u001b[39m\u001b[34m(data, values, index, columns, aggfunc, fill_value, margins, dropna, margins_name, observed, sort, kwargs)\u001b[39m\n\u001b[32m 313\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m values:\n\u001b[32m 314\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m i \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m data:\n\u001b[32m--> \u001b[39m\u001b[32m315\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(i)\n\u001b[32m 317\u001b[39m to_filter = []\n\u001b[32m 318\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m x \u001b[38;5;129;01min\u001b[39;00m keys + values:\n", + "\u001b[31mKeyError\u001b[39m: 'Total Claim Amount'" + ] + } + ], + "source": [ + "sales_channel_revenue = marketing.pivot_table(\n", + " index=\"Sales Channel\",\n", + " values=\"Total Claim Amount\",\n", + " aggfunc=\"sum\"\n", + ").round(2)\n", + "\n", + "sales_channel_revenue" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "5a57d6f2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['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", + " 'month']" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "marketing.columns.tolist()" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "95ba0c9c", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.microsoft.datawrangler.viewer.v0+json": { + "columns": [ + { + "name": "sales_channel", + "rawType": "str", + "type": "string" + }, + { + "name": "total_claim_amount", + "rawType": "float64", + "type": "float" + } + ], + "ref": "25a73d00-75d4-424e-b2a9-0f80a6cae80b", + "rows": [ + [ + "Agent", + "1810226.82" + ], + [ + "Branch", + "1301204.0" + ], + [ + "Call Center", + "926600.82" + ], + [ + "Web", + "706600.04" + ] + ], + "shape": { + "columns": 1, + "rows": 4 + } + }, + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
total_claim_amount
sales_channel
Agent1810226.82
Branch1301204.00
Call Center926600.82
Web706600.04
\n", + "
" + ], + "text/plain": [ + " total_claim_amount\n", + "sales_channel \n", + "Agent 1810226.82\n", + "Branch 1301204.00\n", + "Call Center 926600.82\n", + "Web 706600.04" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sales_channel_revenue = marketing.pivot_table(\n", + " index=\"sales_channel\",\n", + " values=\"total_claim_amount\",\n", + " aggfunc=\"sum\"\n", + ").round(2)\n", + "\n", + "sales_channel_revenue" + ] + }, { "cell_type": "markdown", "id": "640993b2-a291-436c-a34d-a551144f8196", @@ -103,6 +1285,157 @@ "2. Create a pivot table that shows the average customer lifetime value per gender and education level. Analyze the resulting table to draw insights." ] }, + { + "cell_type": "code", + "execution_count": 27, + "id": "e87d553a", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.microsoft.datawrangler.viewer.v0+json": { + "columns": [ + { + "name": "gender", + "rawType": "str", + "type": "string" + }, + { + "name": "Bachelor", + "rawType": "float64", + "type": "float" + }, + { + "name": "College", + "rawType": "float64", + "type": "float" + }, + { + "name": "Doctor", + "rawType": "float64", + "type": "float" + }, + { + "name": "High School or Below", + "rawType": "float64", + "type": "float" + }, + { + "name": "Master", + "rawType": "float64", + "type": "float" + } + ], + "ref": "dadd480e-a18c-487f-98e9-834650eb5254", + "rows": [ + [ + "F", + "7874.27", + "7748.82", + "7328.51", + "8675.22", + "8157.05" + ], + [ + "M", + "7703.6", + "8052.46", + "7415.33", + "8149.69", + "8168.83" + ] + ], + "shape": { + "columns": 5, + "rows": 2 + } + }, + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
educationBachelorCollegeDoctorHigh School or BelowMaster
gender
F7874.277748.827328.518675.228157.05
M7703.608052.467415.338149.698168.83
\n", + "
" + ], + "text/plain": [ + "education Bachelor College Doctor High School or Below Master\n", + "gender \n", + "F 7874.27 7748.82 7328.51 8675.22 8157.05\n", + "M 7703.60 8052.46 7415.33 8149.69 8168.83" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "clv_gender_education = marketing.pivot_table(\n", + " index=\"gender\",\n", + " columns=\"education\",\n", + " values=\"customer_lifetime_value\",\n", + " aggfunc=\"mean\"\n", + ").round(2)\n", + "\n", + "clv_gender_education" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fb98b2c8", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "id": "32c7f2e5-3d90-43e5-be33-9781b6069198", @@ -130,14 +1463,169 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "id": "3a069e0b-b400-470e-904d-d17582191be4", "metadata": { "id": "3a069e0b-b400-470e-904d-d17582191be4" }, - "outputs": [], + "outputs": [ + { + "data": { + "application/vnd.microsoft.datawrangler.viewer.v0+json": { + "columns": [ + { + "name": "index", + "rawType": "int64", + "type": "integer" + }, + { + "name": "policy_type", + "rawType": "str", + "type": "string" + }, + { + "name": "month", + "rawType": "int64", + "type": "integer" + }, + { + "name": "number_of_open_complaints", + "rawType": "float64", + "type": "float" + } + ], + "ref": "6bd7ea31-cee7-48e7-b5f2-74ab8e828a10", + "rows": [ + [ + "0", + "Corporate Auto", + "1", + "443.4349518341929" + ], + [ + "1", + "Corporate Auto", + "2", + "385.2081346696507" + ], + [ + "2", + "Personal Auto", + "1", + "1727.6057215140604" + ], + [ + "3", + "Personal Auto", + "2", + "1453.684440984723" + ], + [ + "4", + "Special Auto", + "1", + "87.07404884693977" + ], + [ + "5", + "Special Auto", + "2", + "95.22681716454218" + ] + ], + "shape": { + "columns": 3, + "rows": 6 + } + }, + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
policy_typemonthnumber_of_open_complaints
0Corporate Auto1443.434952
1Corporate Auto2385.208135
2Personal Auto11727.605722
3Personal Auto21453.684441
4Special Auto187.074049
5Special Auto295.226817
\n", + "
" + ], + "text/plain": [ + " policy_type month number_of_open_complaints\n", + "0 Corporate Auto 1 443.434952\n", + "1 Corporate Auto 2 385.208135\n", + "2 Personal Auto 1 1727.605722\n", + "3 Personal Auto 2 1453.684441\n", + "4 Special Auto 1 87.074049\n", + "5 Special Auto 2 95.226817" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code goes here" + "complaints_by_policy_month = marketing.pivot_table(\n", + " index=[\"policy_type\", \"month\"],\n", + " values=\"number_of_open_complaints\",\n", + " aggfunc=\"sum\"\n", + ").reset_index()\n", + "\n", + "complaints_by_policy_month" ] } ], @@ -146,7 +1634,7 @@ "provenance": [] }, "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -160,7 +1648,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.6" } }, "nbformat": 4,