diff --git a/Copie de lab-dw-aggregating.ipynb b/Copie de lab-dw-aggregating.ipynb new file mode 100644 index 0000000..3f9a523 --- /dev/null +++ b/Copie de lab-dw-aggregating.ipynb @@ -0,0 +1 @@ +{"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":"code","source":["#Our goal is to explore and analyze this data by first performing data cleaning, formatting, and structuring.\n","import pandas as pd\n","url = 'https://raw.githubusercontent.com/data-bootcamp-v4/data/main/marketing_customer_analysis.csv'\n","df = pd.read_csv(url)\n","df"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":739},"id":"M_MTyL43bFVS","executionInfo":{"status":"ok","timestamp":1760910183779,"user_tz":-120,"elapsed":1098,"user":{"displayName":"Rahala Moindze","userId":"02611134585983790642"}},"outputId":"076aaada-ee1a-4b73-9bc0-5cb9f2376fff"},"id":"M_MTyL43bFVS","execution_count":1,"outputs":[{"output_type":"execute_result","data":{"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","10905 10905 FE99816 Nevada 15563.369440 No \n","10906 10906 KX53892 Oregon 5259.444853 No \n","10907 10907 TL39050 Arizona 23893.304100 No \n","10908 10908 WA60547 California 11971.977650 No \n","10909 10909 IV32877 NaN 6857.519928 NaN \n","\n"," Coverage Education Effective To Date EmploymentStatus Gender ... \\\n","0 Basic College 2/18/11 Employed M ... \n","1 Basic College 1/18/11 Unemployed F ... \n","2 Basic Bachelor 2/10/11 Employed M ... \n","3 Extended College 1/11/11 Employed M ... \n","4 Premium Bachelor 1/17/11 Medical Leave F ... \n","... ... ... ... ... ... ... \n","10905 Premium Bachelor 1/19/11 Unemployed F ... \n","10906 Basic College 1/6/11 Employed F ... \n","10907 Extended Bachelor 2/6/11 Employed F ... \n","10908 Premium College 2/13/11 Employed F ... \n","10909 Basic Bachelor 1/8/11 Unemployed M ... \n","\n"," Number of Open Complaints Number of Policies Policy Type \\\n","0 0.0 9 Corporate Auto \n","1 0.0 1 Personal Auto \n","2 0.0 2 Personal Auto \n","3 0.0 2 Corporate Auto \n","4 NaN 7 Personal Auto \n","... ... ... ... \n","10905 NaN 7 Personal Auto \n","10906 0.0 6 Personal Auto \n","10907 0.0 2 Corporate Auto \n","10908 4.0 6 Personal Auto \n","10909 0.0 3 Personal Auto \n","\n"," Policy Renew Offer Type Sales Channel Total Claim Amount \\\n","0 Corporate L3 Offer3 Agent 292.800000 \n","1 Personal L3 Offer4 Call Center 744.924331 \n","2 Personal L3 Offer3 Call Center 480.000000 \n","3 Corporate L3 Offer2 Branch 484.013411 \n","4 Personal L2 Offer1 Branch 707.925645 \n","... ... ... ... ... \n","10905 Personal L1 Offer3 Web 1214.400000 \n","10906 Personal L3 Offer2 Branch 273.018929 \n","10907 Corporate L3 Offer1 Web 381.306996 \n","10908 Personal L1 Offer1 Branch 618.288849 \n","10909 Personal L1 Offer4 Web 1021.719397 \n","\n"," Vehicle Class Vehicle Size Vehicle Type \n","0 Four-Door Car Medsize NaN \n","1 Four-Door Car Medsize NaN \n","2 SUV Medsize A \n","3 Four-Door Car Medsize A \n","4 Four-Door Car Medsize NaN \n","... ... ... ... \n","10905 Luxury Car Medsize A \n","10906 Four-Door Car Medsize A \n","10907 Luxury SUV Medsize NaN \n","10908 SUV Medsize A \n","10909 SUV Medsize NaN \n","\n","[10910 rows x 26 columns]"],"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"," \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 Open ComplaintsNumber of PoliciesPolicy TypePolicyRenew Offer TypeSales ChannelTotal Claim AmountVehicle ClassVehicle SizeVehicle Type
00DK49336Arizona4809.216960NoBasicCollege2/18/11EmployedM...0.09Corporate AutoCorporate L3Offer3Agent292.800000Four-Door CarMedsizeNaN
11KX64629California2228.525238NoBasicCollege1/18/11UnemployedF...0.01Personal AutoPersonal L3Offer4Call Center744.924331Four-Door CarMedsizeNaN
22LZ68649Washington14947.917300NoBasicBachelor2/10/11EmployedM...0.02Personal AutoPersonal L3Offer3Call Center480.000000SUVMedsizeA
33XL78013Oregon22332.439460YesExtendedCollege1/11/11EmployedM...0.02Corporate AutoCorporate L3Offer2Branch484.013411Four-Door CarMedsizeA
44QA50777Oregon9025.067525NoPremiumBachelor1/17/11Medical LeaveF...NaN7Personal AutoPersonal L2Offer1Branch707.925645Four-Door CarMedsizeNaN
..................................................................
1090510905FE99816Nevada15563.369440NoPremiumBachelor1/19/11UnemployedF...NaN7Personal AutoPersonal L1Offer3Web1214.400000Luxury CarMedsizeA
1090610906KX53892Oregon5259.444853NoBasicCollege1/6/11EmployedF...0.06Personal AutoPersonal L3Offer2Branch273.018929Four-Door CarMedsizeA
1090710907TL39050Arizona23893.304100NoExtendedBachelor2/6/11EmployedF...0.02Corporate AutoCorporate L3Offer1Web381.306996Luxury SUVMedsizeNaN
1090810908WA60547California11971.977650NoPremiumCollege2/13/11EmployedF...4.06Personal AutoPersonal L1Offer1Branch618.288849SUVMedsizeA
1090910909IV32877NaN6857.519928NaNBasicBachelor1/8/11UnemployedM...0.03Personal AutoPersonal L1Offer4Web1021.719397SUVMedsizeNaN
\n","

10910 rows × 26 columns

\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"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"df"}},"metadata":{},"execution_count":1}]},{"cell_type":"code","source":["#Performing data cleaning, formatting, and structuring.\n","df.columns = df.columns.str.lower().str.replace(' ', '_')\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":617},"id":"oq-TKyyZbneW","executionInfo":{"status":"ok","timestamp":1760910297858,"user_tz":-120,"elapsed":116,"user":{"displayName":"Rahala Moindze","userId":"02611134585983790642"}},"outputId":"32a18b19-2801-4581-981b-b1a3738a5a1d"},"id":"oq-TKyyZbneW","execution_count":2,"outputs":[{"output_type":"execute_result","data":{"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","10905 10905 FE99816 Nevada 15563.369440 No \n","10906 10906 KX53892 Oregon 5259.444853 No \n","10907 10907 TL39050 Arizona 23893.304100 No \n","10908 10908 WA60547 California 11971.977650 No \n","10909 10909 IV32877 NaN 6857.519928 NaN \n","\n"," coverage education effective_to_date employmentstatus gender ... \\\n","0 Basic College 2/18/11 Employed M ... \n","1 Basic College 1/18/11 Unemployed F ... \n","2 Basic Bachelor 2/10/11 Employed M ... \n","3 Extended College 1/11/11 Employed M ... \n","4 Premium Bachelor 1/17/11 Medical Leave F ... \n","... ... ... ... ... ... ... \n","10905 Premium Bachelor 1/19/11 Unemployed F ... \n","10906 Basic College 1/6/11 Employed F ... \n","10907 Extended Bachelor 2/6/11 Employed F ... \n","10908 Premium College 2/13/11 Employed F ... \n","10909 Basic Bachelor 1/8/11 Unemployed M ... \n","\n"," number_of_open_complaints number_of_policies policy_type \\\n","0 0.0 9 Corporate Auto \n","1 0.0 1 Personal Auto \n","2 0.0 2 Personal Auto \n","3 0.0 2 Corporate Auto \n","4 NaN 7 Personal Auto \n","... ... ... ... \n","10905 NaN 7 Personal Auto \n","10906 0.0 6 Personal Auto \n","10907 0.0 2 Corporate Auto \n","10908 4.0 6 Personal Auto \n","10909 0.0 3 Personal Auto \n","\n"," policy renew_offer_type sales_channel total_claim_amount \\\n","0 Corporate L3 Offer3 Agent 292.800000 \n","1 Personal L3 Offer4 Call Center 744.924331 \n","2 Personal L3 Offer3 Call Center 480.000000 \n","3 Corporate L3 Offer2 Branch 484.013411 \n","4 Personal L2 Offer1 Branch 707.925645 \n","... ... ... ... ... \n","10905 Personal L1 Offer3 Web 1214.400000 \n","10906 Personal L3 Offer2 Branch 273.018929 \n","10907 Corporate L3 Offer1 Web 381.306996 \n","10908 Personal L1 Offer1 Branch 618.288849 \n","10909 Personal L1 Offer4 Web 1021.719397 \n","\n"," vehicle_class vehicle_size vehicle_type \n","0 Four-Door Car Medsize NaN \n","1 Four-Door Car Medsize NaN \n","2 SUV Medsize A \n","3 Four-Door Car Medsize A \n","4 Four-Door Car Medsize NaN \n","... ... ... ... \n","10905 Luxury Car Medsize A \n","10906 Four-Door Car Medsize A \n","10907 Luxury SUV Medsize NaN \n","10908 SUV Medsize A \n","10909 SUV Medsize NaN \n","\n","[10910 rows x 26 columns]"],"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"," \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_open_complaintsnumber_of_policiespolicy_typepolicyrenew_offer_typesales_channeltotal_claim_amountvehicle_classvehicle_sizevehicle_type
00DK49336Arizona4809.216960NoBasicCollege2/18/11EmployedM...0.09Corporate AutoCorporate L3Offer3Agent292.800000Four-Door CarMedsizeNaN
11KX64629California2228.525238NoBasicCollege1/18/11UnemployedF...0.01Personal AutoPersonal L3Offer4Call Center744.924331Four-Door CarMedsizeNaN
22LZ68649Washington14947.917300NoBasicBachelor2/10/11EmployedM...0.02Personal AutoPersonal L3Offer3Call Center480.000000SUVMedsizeA
33XL78013Oregon22332.439460YesExtendedCollege1/11/11EmployedM...0.02Corporate AutoCorporate L3Offer2Branch484.013411Four-Door CarMedsizeA
44QA50777Oregon9025.067525NoPremiumBachelor1/17/11Medical LeaveF...NaN7Personal AutoPersonal L2Offer1Branch707.925645Four-Door CarMedsizeNaN
..................................................................
1090510905FE99816Nevada15563.369440NoPremiumBachelor1/19/11UnemployedF...NaN7Personal AutoPersonal L1Offer3Web1214.400000Luxury CarMedsizeA
1090610906KX53892Oregon5259.444853NoBasicCollege1/6/11EmployedF...0.06Personal AutoPersonal L3Offer2Branch273.018929Four-Door CarMedsizeA
1090710907TL39050Arizona23893.304100NoExtendedBachelor2/6/11EmployedF...0.02Corporate AutoCorporate L3Offer1Web381.306996Luxury SUVMedsizeNaN
1090810908WA60547California11971.977650NoPremiumCollege2/13/11EmployedF...4.06Personal AutoPersonal L1Offer1Branch618.288849SUVMedsizeA
1090910909IV32877NaN6857.519928NaNBasicBachelor1/8/11UnemployedM...0.03Personal AutoPersonal L1Offer4Web1021.719397SUVMedsizeNaN
\n","

10910 rows × 26 columns

\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"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"df"}},"metadata":{},"execution_count":2}]},{"cell_type":"code","source":["pd.to_datetime(df[\"effective_to_date\"], errors=\"ignore\")"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":548},"id":"8fsMLdtgbttb","executionInfo":{"status":"ok","timestamp":1760910943258,"user_tz":-120,"elapsed":49,"user":{"displayName":"Rahala Moindze","userId":"02611134585983790642"}},"outputId":"4a8f793e-1ea0-404d-b273-aa740ab923be"},"id":"8fsMLdtgbttb","execution_count":8,"outputs":[{"output_type":"stream","name":"stderr","text":["/tmp/ipython-input-2338632711.py:1: FutureWarning: errors='ignore' is deprecated and will raise in a future version. Use to_datetime without passing `errors` and catch exceptions explicitly instead\n"," pd.to_datetime(df[\"effective_to_date\"], errors=\"ignore\")\n","/tmp/ipython-input-2338632711.py:1: UserWarning: Could not infer format, so each element will be parsed individually, falling back to `dateutil`. To ensure parsing is consistent and as-expected, please specify a format.\n"," pd.to_datetime(df[\"effective_to_date\"], errors=\"ignore\")\n"]},{"output_type":"execute_result","data":{"text/plain":["2 2011-02-10\n","3 2011-01-11\n","10 2011-01-02\n","13 2011-01-26\n","16 2011-02-07\n"," ... \n","10902 2011-01-24\n","10903 2011-01-07\n","10904 2011-02-24\n","10906 2011-01-06\n","10908 2011-02-13\n","Name: effective_to_date, Length: 4543, dtype: datetime64[ns]"],"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","
effective_to_date
22011-02-10
32011-01-11
102011-01-02
132011-01-26
162011-02-07
......
109022011-01-24
109032011-01-07
109042011-02-24
109062011-01-06
109082011-02-13
\n","

4543 rows × 1 columns

\n","

"]},"metadata":{},"execution_count":8}]},{"cell_type":"code","source":["df = df.dropna()\n","df.isnull().sum()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":899},"id":"9FYlGbnncsSk","executionInfo":{"status":"ok","timestamp":1760910796161,"user_tz":-120,"elapsed":34,"user":{"displayName":"Rahala Moindze","userId":"02611134585983790642"}},"outputId":"33829dca-7534-4910-8bab-5e478da5646b"},"id":"9FYlGbnncsSk","execution_count":6,"outputs":[{"output_type":"execute_result","data":{"text/plain":["unnamed:_0 0\n","customer 0\n","state 0\n","customer_lifetime_value 0\n","response 0\n","coverage 0\n","education 0\n","effective_to_date 0\n","employmentstatus 0\n","gender 0\n","income 0\n","location_code 0\n","marital_status 0\n","monthly_premium_auto 0\n","months_since_last_claim 0\n","months_since_policy_inception 0\n","number_of_open_complaints 0\n","number_of_policies 0\n","policy_type 0\n","policy 0\n","renew_offer_type 0\n","sales_channel 0\n","total_claim_amount 0\n","vehicle_class 0\n","vehicle_size 0\n","vehicle_type 0\n","dtype: int64"],"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","
0
unnamed:_00
customer0
state0
customer_lifetime_value0
response0
coverage0
education0
effective_to_date0
employmentstatus0
gender0
income0
location_code0
marital_status0
monthly_premium_auto0
months_since_last_claim0
months_since_policy_inception0
number_of_open_complaints0
number_of_policies0
policy_type0
policy0
renew_offer_type0
sales_channel0
total_claim_amount0
vehicle_class0
vehicle_size0
vehicle_type0
\n","

"]},"metadata":{},"execution_count":6}]},{"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":"code","source":["#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.\n","df_filtered = df[(df[\"total_claim_amount\"] < 1000) & (df[\"response\"] == \"Yes\")]\n","df_filtered\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":652},"id":"m3SNAD9rd5iu","executionInfo":{"status":"ok","timestamp":1760910949694,"user_tz":-120,"elapsed":54,"user":{"displayName":"Rahala Moindze","userId":"02611134585983790642"}},"outputId":"7bbd3662-7684-4ae0-ca80-42d3dc1eef4b"},"id":"m3SNAD9rd5iu","execution_count":9,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" unnamed:_0 customer state customer_lifetime_value response \\\n","3 3 XL78013 Oregon 22332.439460 Yes \n","19 19 NJ54277 California 3746.751625 Yes \n","69 69 QG27547 Oregon 2867.312197 Yes \n","102 102 VG56765 Arizona 2471.528431 Yes \n","113 113 EC28398 Oregon 5096.673223 Yes \n","... ... ... ... ... ... \n","10818 10818 XT35473 Arizona 2300.691547 Yes \n","10825 10825 FB17016 Oregon 5470.060561 Yes \n","10840 10840 ME22430 Nevada 2453.570794 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","19 Extended College 2/26/11 Employed \n","69 Extended Bachelor 1/3/11 Retired \n","102 Basic High School or Below 1/15/11 Employed \n","113 Basic Master 1/28/11 Disabled \n","... ... ... ... ... \n","10818 Basic Bachelor 1/15/11 Retired \n","10825 Extended Bachelor 2/17/11 Employed \n","10840 Basic Bachelor 2/9/11 Medical Leave \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","19 F ... 1.0 1 \n","69 F ... 0.0 1 \n","102 M ... 0.0 1 \n","113 F ... 0.0 3 \n","... ... ... ... ... \n","10818 M ... 0.0 1 \n","10825 F ... 0.0 1 \n","10840 M ... 0.0 1 \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","19 Personal Auto Personal L2 Offer2 Call Center \n","69 Personal Auto Personal L3 Offer2 Call Center \n","102 Personal Auto Personal L2 Offer2 Agent \n","113 Corporate Auto Corporate L2 Offer1 Agent \n","... ... ... ... ... \n","10818 Personal Auto Personal L3 Offer1 Agent \n","10825 Personal Auto Personal L3 Offer2 Agent \n","10840 Personal Auto Personal L2 Offer1 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","19 19.575683 Two-Door Car Large A \n","69 374.400000 Four-Door Car Medsize A \n","102 114.273025 Two-Door Car Medsize A \n","113 312.000000 Four-Door Car Small A \n","... ... ... ... ... \n","10818 302.400000 Four-Door Car Large A \n","10825 702.990032 SUV Medsize A \n","10840 331.200000 Four-Door Car Medsize A \n","10887 528.200860 SUV Small A \n","10897 158.077504 Sports Car Medsize A \n","\n","[614 rows x 26 columns]"],"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"," \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_open_complaintsnumber_of_policiespolicy_typepolicyrenew_offer_typesales_channeltotal_claim_amountvehicle_classvehicle_sizevehicle_type
33XL78013Oregon22332.439460YesExtendedCollege1/11/11EmployedM...0.02Corporate AutoCorporate L3Offer2Branch484.013411Four-Door CarMedsizeA
1919NJ54277California3746.751625YesExtendedCollege2/26/11EmployedF...1.01Personal AutoPersonal L2Offer2Call Center19.575683Two-Door CarLargeA
6969QG27547Oregon2867.312197YesExtendedBachelor1/3/11RetiredF...0.01Personal AutoPersonal L3Offer2Call Center374.400000Four-Door CarMedsizeA
102102VG56765Arizona2471.528431YesBasicHigh School or Below1/15/11EmployedM...0.01Personal AutoPersonal L2Offer2Agent114.273025Two-Door CarMedsizeA
113113EC28398Oregon5096.673223YesBasicMaster1/28/11DisabledF...0.03Corporate AutoCorporate L2Offer1Agent312.000000Four-Door CarSmallA
..................................................................
1081810818XT35473Arizona2300.691547YesBasicBachelor1/15/11RetiredM...0.01Personal AutoPersonal L3Offer1Agent302.400000Four-Door CarLargeA
1082510825FB17016Oregon5470.060561YesExtendedBachelor2/17/11EmployedF...0.01Personal AutoPersonal L3Offer2Agent702.990032SUVMedsizeA
1084010840ME22430Nevada2453.570794YesBasicBachelor2/9/11Medical LeaveM...0.01Personal AutoPersonal L2Offer1Agent331.200000Four-Door CarMedsizeA
1088710887BY78730Oregon8879.790017YesBasicHigh School or Below2/3/11EmployedF...0.07Special AutoSpecial L2Offer1Agent528.200860SUVSmallA
1089710897MM70762Arizona9075.768214YesBasicMaster1/26/11EmployedM...0.08Personal AutoPersonal L1Offer1Agent158.077504Sports CarMedsizeA
\n","

614 rows × 26 columns

\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"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"df_filtered"}},"metadata":{},"execution_count":9}]},{"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":"code","source":["#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 compare these insights to total_claim_amount patterns, and discuss which segments appear most profitable or low-risk for the company.\n","df_filtered = df[df[\"response\"] == \"Yes\"]\n","df_filtered.groupby([\"policy_type\", \"gender\"])[[\"monthly_premium_auto\", \"customer_lifetime_value\"]].mean()\n","\n","\n","\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":269},"id":"vYyj3BPWeUVw","executionInfo":{"status":"ok","timestamp":1760911115037,"user_tz":-120,"elapsed":68,"user":{"displayName":"Rahala Moindze","userId":"02611134585983790642"}},"outputId":"390a2759-689a-48e6-eddd-3ef327f264b3"},"id":"vYyj3BPWeUVw","execution_count":11,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" monthly_premium_auto customer_lifetime_value\n","policy_type gender \n","Corporate Auto F 93.597403 8400.519583\n"," M 90.819672 7545.446171\n","Personal Auto F 99.100000 7755.844201\n"," M 92.821429 7402.677522\n","Special Auto F 102.294118 8403.318929\n"," M 84.928571 8655.148804"],"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","
monthly_premium_autocustomer_lifetime_value
policy_typegender
Corporate AutoF93.5974038400.519583
M90.8196727545.446171
Personal AutoF99.1000007755.844201
M92.8214297402.677522
Special AutoF102.2941188403.318929
M84.9285718655.148804
\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","\n","
\n"," \n","\n","\n","\n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","summary":"{\n \"name\": \"df_filtered\",\n \"rows\": 6,\n \"fields\": [\n {\n \"column\": \"monthly_premium_auto\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 6.143723829765385,\n \"min\": 84.92857142857143,\n \"max\": 102.29411764705883,\n \"num_unique_values\": 6,\n \"samples\": [\n 93.59740259740259,\n 90.81967213114754,\n 84.92857142857143\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"customer_lifetime_value\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 523.6223252488743,\n \"min\": 7402.677521579365,\n \"max\": 8655.148803714286,\n \"num_unique_values\": 6,\n \"samples\": [\n 8400.519582948053,\n 7545.446171163934,\n 8655.148803714286\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":11}]},{"cell_type":"code","source":["#for customers who responded \"Yes\", and compare these insights to total_claim_amount patterns, and discuss which segments appear most profitable or low-risk for the company.\n","df_filtered = df[df[\"response\"] == \"Yes\"]\n","df_filtered.groupby([\"policy_type\", \"gender\"])[[\"total_claim_amount\"]].mean()\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":269},"id":"X4-pFT2Re_F-","executionInfo":{"status":"ok","timestamp":1760911176379,"user_tz":-120,"elapsed":65,"user":{"displayName":"Rahala Moindze","userId":"02611134585983790642"}},"outputId":"42b62676-6c8d-42f2-a4d3-dbbc189a6d19"},"id":"X4-pFT2Re_F-","execution_count":12,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" total_claim_amount\n","policy_type gender \n","Corporate Auto F 404.104008\n"," M 391.370774\n","Personal Auto F 457.056315\n"," M 472.489180\n","Special Auto F 495.689401\n"," M 368.523035"],"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","
total_claim_amount
policy_typegender
Corporate AutoF404.104008
M391.370774
Personal AutoF457.056315
M472.489180
Special AutoF495.689401
M368.523035
\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","\n","
\n"," \n","\n","\n","\n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","summary":"{\n \"name\": \"df_filtered\",\n \"rows\": 6,\n \"fields\": [\n {\n \"column\": \"total_claim_amount\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 50.557891011578654,\n \"min\": 368.523035,\n \"max\": 495.6894014705882,\n \"num_unique_values\": 6,\n \"samples\": [\n 404.1040077662338,\n 391.3707735409836,\n 368.523035\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":12}]},{"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":"code","source":["#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","df_filtered = df.groupby([\"state\"])[\"customer\"].count()\n","df_filtered[df_filtered > 500]"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":209},"id":"rtoKsNJUfGQJ","executionInfo":{"status":"ok","timestamp":1760911207691,"user_tz":-120,"elapsed":33,"user":{"displayName":"Rahala Moindze","userId":"02611134585983790642"}},"outputId":"1211a818-8ca4-4f29-c124-7d07099c026b"},"id":"rtoKsNJUfGQJ","execution_count":13,"outputs":[{"output_type":"execute_result","data":{"text/plain":["state\n","Arizona 868\n","California 1552\n","Oregon 1281\n","Name: customer, dtype: int64"],"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","
customer
state
Arizona868
California1552
Oregon1281
\n","

"]},"metadata":{},"execution_count":13}]},{"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","source":["#Find the maximum, minimum, and median customer lifetime value by education level and gender. Write your conclusions.\n","df_filtered = df.groupby([\"education\", \"gender\"])[\"customer_lifetime_value\"].agg([\"max\", \"min\", \"median\"])\n","df_filtered"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":394},"id":"WPUTVyf6fM-g","executionInfo":{"status":"ok","timestamp":1760911234551,"user_tz":-120,"elapsed":58,"user":{"displayName":"Rahala Moindze","userId":"02611134585983790642"}},"outputId":"4ef96c83-ae25-4f73-c619-5cacd3af7815"},"id":"WPUTVyf6fM-g","execution_count":14,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" max min median\n","education gender \n","Bachelor F 58753.88046 1904.000852 5752.485185\n"," M 67907.27050 2030.783687 5797.377350\n","College F 61850.18803 2004.350666 5642.596077\n"," M 44795.46942 1918.119700 6005.847375\n","Doctor F 44856.11397 2395.570000 5789.850900\n"," M 32677.34284 2267.604038 5843.989737\n","High School or Below F 55277.44589 2150.178588 5978.754765\n"," M 83325.38119 2132.771675 6081.481291\n","Master F 51016.06704 2417.777032 5714.165863\n"," M 50568.25912 2357.503128 5512.342258"],"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","
maxminmedian
educationgender
BachelorF58753.880461904.0008525752.485185
M67907.270502030.7836875797.377350
CollegeF61850.188032004.3506665642.596077
M44795.469421918.1197006005.847375
DoctorF44856.113972395.5700005789.850900
M32677.342842267.6040385843.989737
High School or BelowF55277.445892150.1785885978.754765
M83325.381192132.7716756081.481291
MasterF51016.067042417.7770325714.165863
M50568.259122357.5031285512.342258
\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"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"df_filtered","summary":"{\n \"name\": \"df_filtered\",\n \"rows\": 10,\n \"fields\": [\n {\n \"column\": \"max\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 14026.787480127306,\n \"min\": 32677.34284,\n \"max\": 83325.38119,\n \"num_unique_values\": 10,\n \"samples\": [\n 51016.06704,\n 67907.2705,\n 32677.34284\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"min\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 193.8744872520534,\n \"min\": 1904.000852,\n \"max\": 2417.777032,\n \"num_unique_values\": 10,\n \"samples\": [\n 2417.777032,\n 2030.783687,\n 2267.604038\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"median\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 173.6336279098286,\n \"min\": 5512.342258,\n \"max\": 6081.481291,\n \"num_unique_values\": 10,\n \"samples\": [\n 5714.165863,\n 5797.377350000001,\n 5843.989737\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":14}]},{"cell_type":"code","source":[],"metadata":{"id":"spdYJDjcfYVu"},"id":"spdYJDjcfYVu","execution_count":null,"outputs":[]},{"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":"code","source":["#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","df_filtered = df.groupby([\"state\", \"effective_to_date\"])[\"number_of_policies\"].sum()\n","df_filtered.unstack()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":287},"id":"_4n-czXFfhFj","executionInfo":{"status":"ok","timestamp":1760911396914,"user_tz":-120,"elapsed":43,"user":{"displayName":"Rahala Moindze","userId":"02611134585983790642"}},"outputId":"5972fbc3-83d0-49d8-9610-a47d29b00148"},"id":"_4n-czXFfhFj","execution_count":16,"outputs":[{"output_type":"execute_result","data":{"text/plain":["effective_to_date 1/1/11 1/10/11 1/11/11 1/12/11 1/13/11 1/14/11 \\\n","state \n","Arizona 55 58 32 49 62 31 \n","California 75 111 121 64 92 64 \n","Nevada 29 10 18 22 12 15 \n","Oregon 71 127 35 38 56 53 \n","Washington 18 30 14 15 16 34 \n","\n","effective_to_date 1/15/11 1/16/11 1/17/11 1/18/11 ... 2/26/11 2/27/11 \\\n","state ... \n","Arizona 45 25 50 70 ... 39 33 \n","California 66 79 58 117 ... 101 51 \n","Nevada 23 8 12 23 ... 21 26 \n","Oregon 47 81 97 77 ... 57 45 \n","Washington 8 15 23 16 ... 13 23 \n","\n","effective_to_date 2/28/11 2/3/11 2/4/11 2/5/11 2/6/11 2/7/11 2/8/11 \\\n","state \n","Arizona 40 79 63 71 38 27 51 \n","California 57 86 70 104 71 100 33 \n","Nevada 41 34 18 14 7 24 42 \n","Oregon 58 82 76 80 67 94 47 \n","Washington 33 21 21 31 23 27 9 \n","\n","effective_to_date 2/9/11 \n","state \n","Arizona 30 \n","California 68 \n","Nevada 8 \n","Oregon 40 \n","Washington 14 \n","\n","[5 rows x 59 columns]"],"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"," \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","
effective_to_date1/1/111/10/111/11/111/12/111/13/111/14/111/15/111/16/111/17/111/18/11...2/26/112/27/112/28/112/3/112/4/112/5/112/6/112/7/112/8/112/9/11
state
Arizona55583249623145255070...39334079637138275130
California75111121649264667958117...10151578670104711003368
Nevada2910182212152381223...212641341814724428
Oregon711273538565347819777...57455882768067944740
Washington1830141516348152316...1323332121312327914
\n","

5 rows × 59 columns

\n","
\n","
\n","\n","
\n"," \n","\n"," \n","\n"," \n","
\n","\n","\n","
\n"," \n","\n","\n","\n"," \n","
\n","\n","
\n","
\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe"}},"metadata":{},"execution_count":16}]},{"cell_type":"code","source":["df = df.drop_duplicates()\n","df.duplicated().sum()\n","df"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":687},"id":"8VCTteGTgQCW","executionInfo":{"status":"ok","timestamp":1760911526859,"user_tz":-120,"elapsed":68,"user":{"displayName":"Rahala Moindze","userId":"02611134585983790642"}},"outputId":"ad3f5bbf-1b92-4678-c34a-d0d9c297a8e0"},"id":"8VCTteGTgQCW","execution_count":18,"outputs":[{"output_type":"execute_result","data":{"text/plain":[" unnamed:_0 customer state customer_lifetime_value response \\\n","2 2 LZ68649 Washington 14947.917300 No \n","3 3 XL78013 Oregon 22332.439460 Yes \n","10 10 HG93801 Arizona 5154.764074 No \n","13 13 KR82385 California 5454.587929 No \n","16 16 FH51383 California 5326.677654 No \n","... ... ... ... ... ... \n","10902 10902 PP30874 California 3579.023825 No \n","10903 10903 SU71163 Arizona 2771.663013 No \n","10904 10904 QI63521 Nevada 19228.463620 No \n","10906 10906 KX53892 Oregon 5259.444853 No \n","10908 10908 WA60547 California 11971.977650 No \n","\n"," coverage education effective_to_date employmentstatus \\\n","2 Basic Bachelor 2/10/11 Employed \n","3 Extended College 1/11/11 Employed \n","10 Extended High School or Below 1/2/11 Employed \n","13 Basic Master 1/26/11 Employed \n","16 Basic High School or Below 2/7/11 Employed \n","... ... ... ... ... \n","10902 Extended High School or Below 1/24/11 Employed \n","10903 Basic College 1/7/11 Employed \n","10904 Basic High School or Below 2/24/11 Unemployed \n","10906 Basic College 1/6/11 Employed \n","10908 Premium College 2/13/11 Employed \n","\n"," gender ... number_of_open_complaints number_of_policies \\\n","2 M ... 0.0 2 \n","3 M ... 0.0 2 \n","10 M ... 0.0 1 \n","13 M ... 0.0 4 \n","16 F ... 0.0 6 \n","... ... ... ... ... \n","10902 F ... 2.0 1 \n","10903 M ... 4.0 1 \n","10904 M ... 0.0 2 \n","10906 F ... 0.0 6 \n","10908 F ... 4.0 6 \n","\n"," policy_type policy renew_offer_type sales_channel \\\n","2 Personal Auto Personal L3 Offer3 Call Center \n","3 Corporate Auto Corporate L3 Offer2 Branch \n","10 Corporate Auto Corporate L3 Offer2 Branch \n","13 Personal Auto Personal L3 Offer4 Call Center \n","16 Personal Auto Personal L3 Offer4 Call Center \n","... ... ... ... ... \n","10902 Personal Auto Personal L2 Offer2 Agent \n","10903 Personal Auto Personal L2 Offer2 Branch \n","10904 Personal Auto Personal L2 Offer1 Branch \n","10906 Personal Auto Personal L3 Offer2 Branch \n","10908 Personal Auto Personal L1 Offer1 Branch \n","\n"," total_claim_amount vehicle_class vehicle_size vehicle_type \n","2 480.000000 SUV Medsize A \n","3 484.013411 Four-Door Car Medsize A \n","10 442.521087 SUV Large A \n","13 331.200000 Two-Door Car Medsize A \n","16 300.528579 Two-Door Car Large A \n","... ... ... ... ... \n","10902 655.200000 Four-Door Car Medsize A \n","10903 355.200000 Two-Door Car Medsize A \n","10904 897.600000 Luxury SUV Medsize A \n","10906 273.018929 Four-Door Car Medsize A \n","10908 618.288849 SUV Medsize A \n","\n","[4543 rows x 26 columns]"],"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"," \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_open_complaintsnumber_of_policiespolicy_typepolicyrenew_offer_typesales_channeltotal_claim_amountvehicle_classvehicle_sizevehicle_type
22LZ68649Washington14947.917300NoBasicBachelor2/10/11EmployedM...0.02Personal AutoPersonal L3Offer3Call Center480.000000SUVMedsizeA
33XL78013Oregon22332.439460YesExtendedCollege1/11/11EmployedM...0.02Corporate AutoCorporate L3Offer2Branch484.013411Four-Door CarMedsizeA
1010HG93801Arizona5154.764074NoExtendedHigh School or Below1/2/11EmployedM...0.01Corporate AutoCorporate L3Offer2Branch442.521087SUVLargeA
1313KR82385California5454.587929NoBasicMaster1/26/11EmployedM...0.04Personal AutoPersonal L3Offer4Call Center331.200000Two-Door CarMedsizeA
1616FH51383California5326.677654NoBasicHigh School or Below2/7/11EmployedF...0.06Personal AutoPersonal L3Offer4Call Center300.528579Two-Door CarLargeA
..................................................................
1090210902PP30874California3579.023825NoExtendedHigh School or Below1/24/11EmployedF...2.01Personal AutoPersonal L2Offer2Agent655.200000Four-Door CarMedsizeA
1090310903SU71163Arizona2771.663013NoBasicCollege1/7/11EmployedM...4.01Personal AutoPersonal L2Offer2Branch355.200000Two-Door CarMedsizeA
1090410904QI63521Nevada19228.463620NoBasicHigh School or Below2/24/11UnemployedM...0.02Personal AutoPersonal L2Offer1Branch897.600000Luxury SUVMedsizeA
1090610906KX53892Oregon5259.444853NoBasicCollege1/6/11EmployedF...0.06Personal AutoPersonal L3Offer2Branch273.018929Four-Door CarMedsizeA
1090810908WA60547California11971.977650NoPremiumCollege2/13/11EmployedF...4.06Personal AutoPersonal L1Offer1Branch618.288849SUVMedsizeA
\n","

4543 rows × 26 columns

\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"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"df"}},"metadata":{},"execution_count":18}]},{"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":"code","source":["#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","# Group by state and sum the number of policies to find the total for each state\n","state_policy_counts = df.groupby(\"state\")[\"number_of_policies\"].sum()\n","\n","# Sort the states by the total number of policies in descending order and select the top 3\n","top_3_states = state_policy_counts.sort_values(ascending=False).head(3).index\n","\n","# Filter the original DataFrame to include only the top 3 states\n","df_top_states = df[df[\"state\"].isin(top_3_states)]\n","\n","# Group the filtered data by state and month, sum the policies, and unstack to get the desired format\n","df_policies_by_month_state = df_top_states.groupby([\"state\", df_top_states[\"effective_to_date\"].str.split('/').str[0]])[\"number_of_policies\"].sum().unstack()\n","\n","df_policies_by_month_state"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":175},"id":"xbCYaw-4f82Z","executionInfo":{"status":"ok","timestamp":1760911572424,"user_tz":-120,"elapsed":51,"user":{"displayName":"Rahala Moindze","userId":"02611134585983790642"}},"outputId":"b3f5490d-12ae-425d-aa71-6fe57577ea72"},"id":"xbCYaw-4f82Z","execution_count":20,"outputs":[{"output_type":"execute_result","data":{"text/plain":["effective_to_date 1 2\n","state \n","Arizona 1344 1297\n","California 2439 2158\n","Oregon 2009 1885"],"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","
effective_to_date12
state
Arizona13441297
California24392158
Oregon20091885
\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"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"df_policies_by_month_state","summary":"{\n \"name\": \"df_policies_by_month_state\",\n \"rows\": 3,\n \"fields\": [\n {\n \"column\": \"state\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"Arizona\",\n \"California\",\n \"Oregon\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"1\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 551,\n \"min\": 1344,\n \"max\": 2439,\n \"num_unique_values\": 3,\n \"samples\": [\n 1344,\n 2439,\n 2009\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"2\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 439,\n \"min\": 1297,\n \"max\": 2158,\n \"num_unique_values\": 3,\n \"samples\": [\n 1297,\n 2158,\n 1885\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"}},"metadata":{},"execution_count":20}]},{"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":21,"id":"449513f4-0459-46a0-a18d-9398d974c9ad","metadata":{"id":"449513f4-0459-46a0-a18d-9398d974c9ad","colab":{"base_uri":"https://localhost:8080/","height":241},"executionInfo":{"status":"ok","timestamp":1760911665530,"user_tz":-120,"elapsed":60,"user":{"displayName":"Rahala Moindze","userId":"02611134585983790642"}},"outputId":"ee71dc5a-e2e2-4d16-a46d-2b463b405372"},"outputs":[{"output_type":"execute_result","data":{"text/plain":["sales_channel\n","Agent 332\n","Branch 123\n","Call Center 97\n","Web 89\n","Name: response, dtype: int64"],"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","
response
sales_channel
Agent332
Branch123
Call Center97
Web89
\n","

"]},"metadata":{},"execution_count":21}],"source":["# The marketing team wants to analyze the effect of different marketing channels on the customer response rate. 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","df_filtered = df[df[\"response\"] == \"Yes\"]\n","df_filtered = df_filtered.groupby([\"sales_channel\"])[\"response\"].count()\n","df_filtered"]}],"metadata":{"colab":{"provenance":[{"file_id":"1Zh_-RoDo-Y0W9BYFP8newGqJMRMhmtrE","timestamp":1760911673849}]},"kernelspec":{"display_name":"Python 3 (ipykernel)","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.9.13"}},"nbformat":4,"nbformat_minor":5} \ No newline at end of file