File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed
LeetCode SQL 50 Solution/585. Investments in 2016 Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change 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 ]})
You can’t perform that action at this time.
0 commit comments