Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c17344e

Browse files
more
1 parent 68bb7b1 commit c17344e

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

‎Medium/585. Investments in 2016.py‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Problem 585: Investments in 2016
2+
# Difficulty: Medium
3+
4+
# Table: Insurance
5+
6+
# +-------------+-------+
7+
# | Column Name | Type |
8+
# +-------------+-------+
9+
# | pid | int |
10+
# | tiv_2015 | float |
11+
# | tiv_2016 | float |
12+
# | lat | float |
13+
# | lon | float |
14+
# +-------------+-------+
15+
# pid is the primary key (column with unique values) for this table.
16+
# Each row of this table contains information about one policy where:
17+
# pid is the policyholder's policy ID.
18+
# tiv_2015 is the total investment value in 2015 and tiv_2016 is the total investment value in 2016.
19+
# lat is the latitude of the policy holder's city. It's guaranteed that lat is not NULL.
20+
# lon is the longitude of the policy holder's city. It's guaranteed that lon is not NULL.
21+
22+
# Problem Statement:
23+
# Write a function to report the sum of all total investment values in 2016 tiv_2016,
24+
# for all policyholders who:
25+
# - have the same tiv_2015 value as one or more other policyholders, and
26+
# - are not located in the same city as any other policyholder (i.e., the (lat, lon) attribute pairs must be unique).
27+
# Round tiv_2016 to two decimal places.
28+
29+
# Solution
30+
31+
import pandas as pd
32+
33+
def find_investments(insurance: pd.DataFrame) -> pd.DataFrame:
34+
# I create a column counting how many times each tiv_2015 value appears
35+
insurance['tiv_2015_cnt'] = insurance.groupby('tiv_2015')['tiv_2015'].transform('count')
36+
37+
# I create another column counting how many times each (lat, lon) pair appears
38+
insurance['latlon_pair_cnt'] = insurance.groupby(['lat', 'lon'])['pid'].transform('count')
39+
40+
# I filter to records where tiv_2015 is shared by at least one other policyholder
41+
# and (lat, lon) is unique
42+
temp_df = insurance.loc[(insurance['tiv_2015_cnt'] > 1) & (insurance['latlon_pair_cnt'] == 1), ]
43+
44+
# I compute and return the sum of tiv_2016 rounded to 2 decimal places
45+
result_df = pd.DataFrame({'tiv_2016': [round(temp_df['tiv_2016'].sum(), 2)]})
46+
return result_df
47+
48+
# Intuition:
49+
# I need to sum the 2016 investment values (tiv_2016) for only those policyholders
50+
# who share their 2015 investment value with others but live in a unique (lat, lon) location.
51+
52+
# Explanation:
53+
# First, I use `groupby().transform('count')` to calculate how many times each tiv_2015 value occurs.
54+
# Then, I check for uniqueness of (lat, lon) by counting how many policyholders are in each city.
55+
# The condition is to keep only those with tiv_2015 repeated more than once and (lat, lon) seen exactly once.
56+
# After filtering, I compute the sum of tiv_2016 for these filtered rows and round the result to two decimal places.
57+
# Problem 585: Investments in 2016
58+
# Difficulty: Medium
59+
60+
# Table: Insurance
61+
62+
# +-------------+-------+
63+
# | Column Name | Type |
64+
# +-------------+-------+
65+
# | pid | int |
66+
# | tiv_2015 | float |
67+
# | tiv_2016 | float |
68+
# | lat | float |
69+
# | lon | float |
70+
# +-------------+-------+
71+
# pid is the primary key (column with unique values) for this table.
72+
# Each row of this table contains information about one policy where:
73+
# pid is the policyholder's policy ID.
74+
# tiv_2015 is the total investment value in 2015 and tiv_2016 is the total investment value in 2016.
75+
# lat is the latitude of the policy holder's city. It's guaranteed that lat is not NULL.
76+
# lon is the longitude of the policy holder's city. It's guaranteed that lon is not NULL.
77+
78+
# Problem Statement:
79+
# Write a function to report the sum of all total investment values in 2016 tiv_2016,
80+
# for all policyholders who:
81+
# - have the same tiv_2015 value as one or more other policyholders, and
82+
# - are not located in the same city as any other policyholder (i.e., the (lat, lon) attribute pairs must be unique).
83+
# Round tiv_2016 to two decimal places.
84+
85+
# Solution
86+
87+
import pandas as pd
88+
89+
def find_investments(insurance: pd.DataFrame) -> pd.DataFrame:
90+
# I create a column counting how many times each tiv_2015 value appears
91+
insurance['tiv_2015_cnt'] = insurance.groupby('tiv_2015')['tiv_2015'].transform('count')
92+
93+
# I create another column counting how many times each (lat, lon) pair appears
94+
insurance['latlon_pair_cnt'] = insurance.groupby(['lat', 'lon'])['pid'].transform('count')
95+
96+
# I filter to records where tiv_2015 is shared by at least one other policyholder
97+
# and (lat, lon) is unique
98+
temp_df = insurance.loc[(insurance['tiv_2015_cnt'] > 1) & (insurance['latlon_pair_cnt'] == 1), ]
99+
100+
# I compute and return the sum of tiv_2016 rounded to 2 decimal places
101+
result_df = pd.DataFrame({'tiv_2016': [round(temp_df['tiv_2016'].sum(), 2)]})
102+
return result_df
103+
104+
# Intuition:
105+
# I need to sum the 2016 investment values (tiv_2016) for only those policyholders
106+
# who share their 2015 investment value with others but live in a unique (lat, lon) location.
107+
108+
# Explanation:
109+
# First, I use `groupby().transform('count')` to calculate how many times each tiv_2015 value occurs.
110+
# Then, I check for uniqueness of (lat, lon) by counting how many policyholders are in each city.
111+
# The condition is to keep only those with tiv_2015 repeated more than once and (lat, lon) seen exactly once.
112+
# After filtering, I compute the sum of tiv_2016 for these filtered rows and round the result to two decimal places.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /