def main(): dc = datacommons.Client() # Get lat/long of a city. query = (""" SELECT ?id ?lat ?long, typeOf ?o City, name ?o 'San Luis Obispo', dcid ?o ?id, latitude ?o ?lat, longitude ?o ?long """) print('Issuing query "{}"'.format(query)) try: df = dc.query(query) except RuntimeError as e: print(e) return with pd.option_context('display.width', 400, 'display.max_rows', 100): print(df) saved_file_name = dc.save_dataframe(df, 'test_df') print(saved_file_name) saved_df = dc.read_dataframe(saved_file_name) assert df.equals(saved_df)
def main(): dc = datacommons.Client() # Bootstrap with IDs of a few US cities. pd_table = pd.DataFrame({ 'city': ['City', 'dc/ve1tlm', 'dc/0vypck3', 'dc/prehdd2'] }) # Add names of those cities. weather_table = dc.expand(pd_table, 'name', 'city', 'city_name') with pd.option_context('display.width', 400, 'display.max_rows', 100): print weather_table # Add monthly mean temperature for those cities for all 2017 months. for d in range(1, 13): weather_table = dc.get_observations( weather_table, seed_col_name='city', new_col_name=('temp_2017%.2d' % d), start_date=('2017-%.2d-01' % d), end_date=('2017-%.2d-01' % d), measured_property='temperature', stats_type='mean') with pd.option_context('display.width', 400, 'display.max_rows', 100): print weather_table
def main(): dc = datacommons.Client() # Build a table with a single US state state_table = dc.get_states('United States', 'state', max_rows=1) # Add the state name and the 5 counties contained in that state state_table = dc.expand(state_table, 'name', 'state', 'state name', outgoing=True) state_table = dc.expand(state_table, 'containedInPlace', 'state', 'county', outgoing=False, max_rows=3) state_table = dc.expand(state_table, 'name', 'county', 'county name', outgoing=True) state_table = dc.get_populations(state_table, seed_col_name='county', new_col_name='county population', population_type='Person', max_rows=100) with pd.option_context('display.width', 400, 'display.max_rows', 100): print state_table state_table = dc.get_populations( state_table, seed_col_name='county', new_col_name='county_18_24_years_population', population_type='Person', max_rows=100, age='USC/18To24Years') with pd.option_context('display.width', 400, 'display.max_rows', 100): print state_table state_table = dc.get_populations(state_table, seed_col_name='county', new_col_name='county male population', population_type='Person', max_rows=100, gender='Male') with pd.option_context('display.width', 400, 'display.max_rows', 100): print state_table state_table = dc.get_observations(state_table, seed_col_name='county population', new_col_name='county person count', observation_date='2016', measured_property='count') with pd.option_context('display.width', 400, 'display.max_rows', 100): print state_table
def main(): dc = datacommons.Client() # Get lat/long of a city. query = (""" SELECT ?id ?lat ?long, typeOf ?o City, name ?o 'San Luis Obispo', dcid ?o ?id, latitude ?o ?lat, longitude ?o ?long """) print 'Issuing query "{}"'.format(query) try: df = dc.Query(query) except RuntimeError as e: print e return with pd.option_context('display.width', 400, 'display.max_rows', 100): print df
def main(): dc = datacommons.Client() # Start with all states in the United States and add the state names. This # is an outgoing property of State. pd_state = dc.get_states('United States', 'state') pd_state = dc.expand(pd_state, 'name', 'state', 'state_name', outgoing=True) # Add information for counties contained in states in the 'state' column. # Getting the county is an incoming property of State. Note that there are # roughly 3100 counties in the United States pd_state = dc.expand(pd_state, 'containedInPlace', 'state', 'county', outgoing=False, max_rows=50) pd_state = dc.expand(pd_state, 'name', 'county', 'county_name', outgoing=True, max_rows=50) # Print out the final data frame with pd.option_context('display.width', 400, 'display.max_rows', 100): print pd_state
def main(): dc = datacommons.Client() # Start with all states in the United States and add the state names. This # is an outgoing property of State. pd_state = dc.get_places_in( place_type='State', container_dcid='dc/2sffw13', # United States col_name='state') pd_state = dc.expand(pd_state, 'name', 'state', 'state_name', outgoing=True) # Add information for counties contained in states in the 'state' column. # Getting the county is an incoming property of State. Note that there are # roughly 3100 counties in the United States pd_state = dc.expand( pd_state, 'containedInPlace', 'state', 'county', outgoing=False, max_rows=50) pd_state = dc.expand( pd_state, 'name', 'county', 'county_name', outgoing=True, max_rows=50) # Print out the final data frame with pd.option_context('display.width', 400, 'display.max_rows', 100): print pd_state pd_city = dc.get_places_in( place_type='City', container_dcid='dc/b72vdv', # California col_name='city') pd_city = dc.expand(pd_city, 'name', 'city', 'city_name', outgoing=True) with pd.option_context('display.width', 400, 'display.max_rows', 100): print pd_city
def main(): dc = datacommons.Client() # Get a list of "Class" type instance. pd_class = dc.get_instances('class', 'Class', max_rows=_MAX_ROW) with pd.option_context('display.width', 400, 'display.max_rows', 20): print pd_class # Get a list of states with their names pd_state = dc.get_instances('state', 'State', max_rows=_MAX_ROW) pd_state = dc.expand(pd_state, 'name', 'state', 'state_name', outgoing=True) with pd.option_context('display.width', 400, 'display.max_rows', 20): print pd_state # Get a list of cities with their names and timezone. pd_city = dc.get_instances('city', 'City', max_rows=_MAX_ROW) pd_city = dc.expand(pd_city, 'name', 'city', 'name', outgoing=True) pd_city = dc.expand(pd_city, 'timezone', 'city', 'timezone', outgoing=True) with pd.option_context('display.width', 400, 'display.max_rows', 20): print pd_city