dylanplummer commited on
Commit
4520d60
·
1 Parent(s): 7ffa3df

fix county map

Browse files
Files changed (1) hide show
  1. app.py +20 -8
app.py CHANGED
@@ -38,8 +38,15 @@ alpha_2_map = iso['Alpha-3 code'].to_dict()
38
  with open('counties.json') as f:
39
  counties = json.load(f)
40
 
 
41
  cities = pd.read_csv('uscities.csv')
42
- cities.set_index('city', inplace=True)
 
 
 
 
 
 
43
  city_county_map = cities['county_fips'].to_dict()
44
  city_county_name_map = cities['county_name'].to_dict()
45
 
@@ -66,14 +73,14 @@ def full_report():
66
  Dimension(name="continent"),
67
  Dimension(name="country"),
68
  Dimension(name="countryId"),
69
- Dimension(name="city")],
70
  metrics=[Metric(name="eventValue")],
71
  #return_property_quota=True,
72
  date_ranges=[DateRange(start_date="2023-06-30", end_date="today")],
73
  )
74
  response = client.run_report(request)
75
 
76
- res = {'day': [], 'jumps': [], 'continent': [], 'country': [], 'iso': [], 'city': []}
77
 
78
  for row in response.rows:
79
  event_name = row.dimension_values[1].value
@@ -89,7 +96,7 @@ def full_report():
89
  res['continent'].append(continent)
90
  res['country'].append(country)
91
  res['iso'].append(country_iso)
92
- res['city'].append(city)
93
 
94
  df = pd.DataFrame.from_dict(res)
95
  cached_report = df.copy(deep=True)
@@ -99,13 +106,16 @@ def full_report():
99
 
100
  total_jumps = int(df['jumps'].sum())
101
  unique_countries = df['country'].nunique()
102
- unique_cities = df['city'].nunique()
103
 
104
  print(f"Total jumps: {total_jumps}, unique countries: {unique_countries}, unique cities: {unique_cities}")
105
  df['iso'] = df['iso'].map(alpha_2_map)
106
  df['jumps'] = df['jumps'].astype(int)
 
 
107
  df['city'] = df.apply(lambda row: row['city'] if row['country'] != 'Bermuda' else 'Hamilton', axis=1)
108
-
 
109
  country_df = df.groupby(['country', 'iso']).sum().reset_index()
110
  country_df = country_df.sort_values(by=['jumps'], ascending=False)
111
  top_10_countries = country_df.iloc[:10]['country'].tolist()
@@ -148,7 +158,7 @@ def full_report():
148
  color="rank",
149
  hover_name="country", # column to add to hover information
150
  hover_data=["jumps"],
151
- color_continuous_scale ="YlOrRd_r",
152
  projection='natural earth',
153
  template="plotly_dark")
154
  # remove the legend
@@ -160,8 +170,10 @@ def full_report():
160
  county_df['count_name'] = county_df['city'].map(city_county_name_map)
161
  county_df = county_df.groupby(['county', 'count_name']).sum().reset_index()
162
  county_df['rank'] = county_df['jumps'].rank(ascending=False)
 
 
163
  county_map = px.choropleth(county_df, geojson=counties, locations='county', color='rank',
164
- color_continuous_scale="YlOrRd_r",
165
  scope="usa",
166
  hover_name="count_name",
167
  hover_data=["jumps"],
 
38
  with open('counties.json') as f:
39
  counties = json.load(f)
40
 
41
+ ga_cities = pd.read_csv('cities.csv')
42
  cities = pd.read_csv('uscities.csv')
43
+ cities['full_city'] = cities['city'] + ', ' + cities['state_name']
44
+ cities.set_index('full_city', inplace=True)
45
+ ga_cities['Criteria ID'] = ga_cities['Criteria ID'].astype(str)
46
+ ga_cities.set_index('Criteria ID', inplace=True)
47
+ ga_city_map = ga_cities['Name'].to_dict()
48
+ ga_cities['state'] = ga_cities['Canonical Name'].str.split(',').str[1].str.strip()
49
+ ga_state_map = ga_cities['state'].to_dict()
50
  city_county_map = cities['county_fips'].to_dict()
51
  city_county_name_map = cities['county_name'].to_dict()
52
 
 
73
  Dimension(name="continent"),
74
  Dimension(name="country"),
75
  Dimension(name="countryId"),
76
+ Dimension(name="cityId")],
77
  metrics=[Metric(name="eventValue")],
78
  #return_property_quota=True,
79
  date_ranges=[DateRange(start_date="2023-06-30", end_date="today")],
80
  )
81
  response = client.run_report(request)
82
 
83
+ res = {'day': [], 'jumps': [], 'continent': [], 'country': [], 'iso': [], 'cityId': []}
84
 
85
  for row in response.rows:
86
  event_name = row.dimension_values[1].value
 
96
  res['continent'].append(continent)
97
  res['country'].append(country)
98
  res['iso'].append(country_iso)
99
+ res['cityId'].append(city)
100
 
101
  df = pd.DataFrame.from_dict(res)
102
  cached_report = df.copy(deep=True)
 
106
 
107
  total_jumps = int(df['jumps'].sum())
108
  unique_countries = df['country'].nunique()
109
+ unique_cities = df['cityId'].nunique()
110
 
111
  print(f"Total jumps: {total_jumps}, unique countries: {unique_countries}, unique cities: {unique_cities}")
112
  df['iso'] = df['iso'].map(alpha_2_map)
113
  df['jumps'] = df['jumps'].astype(int)
114
+ df['city'] = df['cityId'].map(ga_city_map)
115
+ df['state'] = df['cityId'].map(ga_state_map)
116
  df['city'] = df.apply(lambda row: row['city'] if row['country'] != 'Bermuda' else 'Hamilton', axis=1)
117
+ df['city'] = df['city'] + ', ' + df['state']
118
+
119
  country_df = df.groupby(['country', 'iso']).sum().reset_index()
120
  country_df = country_df.sort_values(by=['jumps'], ascending=False)
121
  top_10_countries = country_df.iloc[:10]['country'].tolist()
 
158
  color="rank",
159
  hover_name="country", # column to add to hover information
160
  hover_data=["jumps"],
161
+ color_continuous_scale ="OrRd_r",
162
  projection='natural earth',
163
  template="plotly_dark")
164
  # remove the legend
 
170
  county_df['count_name'] = county_df['city'].map(city_county_name_map)
171
  county_df = county_df.groupby(['county', 'count_name']).sum().reset_index()
172
  county_df['rank'] = county_df['jumps'].rank(ascending=False)
173
+ county_df['county'] = county_df['county'].astype(int)
174
+ county_df['county'] = county_df['county'].astype(str).str.zfill(5) # county codes are two digits for state, three for county
175
  county_map = px.choropleth(county_df, geojson=counties, locations='county', color='rank',
176
+ color_continuous_scale="OrRd_r",
177
  scope="usa",
178
  hover_name="count_name",
179
  hover_data=["jumps"],