Skip to content

Commit c329b6f

Browse files
committed
Update 585. Investments in 2016.py
1 parent ed60162 commit c329b6f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pandas as pd
2+
3+
def investments_in_2016(insurance: pd.DataFrame) -> pd.DataFrame:
4+
# Count the number of occurrences for each tiv_2015 value
5+
insurance['tiv_2015_count'] = insurance.groupby('tiv_2015')['tiv_2015'].transform('count')
6+
7+
# Count the number of occurrences for each (lat, lon) pair
8+
insurance['city_count'] = insurance.groupby(['lat', 'lon'])['lat'].transform('count')
9+
10+
# Filter rows that meet both criteria:
11+
# 1. tiv_2015 appears more than once.
12+
# 2. The location (lat, lon) is unique (appears only once).
13+
valid_rows = insurance[(insurance['tiv_2015_count'] > 1) & (insurance['city_count'] == 1)]
14+
15+
# Calculate the sum of tiv_2016 and round to 2 decimal places
16+
total_tiv_2016 = round(valid_rows['tiv_2016'].sum(), 2)
17+
18+
# Return result as a DataFrame
19+
return pd.DataFrame({'tiv_2016': [total_tiv_2016]})

0 commit comments

Comments
 (0)