-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_commons.py
85 lines (74 loc) · 2.56 KB
/
data_commons.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import datacommons_pandas as dc
def get_variables_for_dcid(dcid_list, variable_name_list):
_df = dc.build_multivariate_dataframe(dcid_list, variable_name_list)
_df.insert(0, 'Name', _df.index.map(dc.get_property_values(_df.index, 'name')))
_df['Name'] = _df['Name'].str[0]
return _df
def get_time_series_dataframe_for_dcid(dcid_list, variable_name):
_df = dc.build_time_series_dataframe(dcid_list, variable_name)
_df.insert(0, 'Name', _df.index.map(dc.get_property_values(_df.index, 'name')))
_df['Name'] = _df['Name'].str[0]
# columns = _df.columns.to_list().remove('Name')
columns = _df.columns.to_list()
columns.remove('Name')
_df = _df.melt(
['Name'],
columns,
'Date',
variable_name,
)
_df = _df.dropna()
_df = _df.drop_duplicates(keep='first')
_df.variable_name = variable_name
return _df
def get_dcid_from_county_name(county_name):
simple_query = f"""
SELECT ?geoId
WHERE {{
?county typeOf County .
?county name '{county_name}' .
?county dcid ?geoId .
}}
LIMIT 1
"""
try:
# Execute the simple query
dcid_dict = dc.query(simple_query)
dcid = [ item['?geoId'] for item in dcid_dict ]
return dcid[0]
except Exception as ex:
return None
def get_dcid_from_state_name(state_name):
simple_query = f"""
SELECT ?geoId
WHERE {{
?state typeOf State .
?state name '{state_name}' .
?state dcid ?geoId .
}}
LIMIT 1
"""
try:
# Execute the simple query
dcid_dict = dc.query(simple_query)
dcid = [ item['?geoId'] for item in dcid_dict ]
return dcid[0]
except Exception as ex:
return None
def get_dcid_from_country_name(country_name):
simple_query = f"""
SELECT ?geoId
WHERE {{
?country typeOf Country .
?country name '{country_name}' .
?country dcid ?geoId .
}}
LIMIT 1
"""
try:
# Execute the simple query
dcid_dict = dc.query(simple_query)
dcid = [ item['?geoId'] for item in dcid_dict ]
return dcid[0]
except Exception as ex:
return None