datacommonsorg / docsite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add a helper function for getting the parent location (inverse of get_places_in)

johnmicahreid opened this issue · comments

Currently there's no way to get the parent location or area for a given place, although you can find the places within a place.

The actual problem I faced was using the CDC 500 City list and trying to find the state that each city is in.

Here's my rather hacky workaround for doing it from the other direction, which involves finding another list of places and merging:

# Get the 500 cities in the CDC500 list
cdc500_dcids = pd.DataFrame(
    dc.get_property_values(["CDC500_City"], "member", limit=500)["CDC500_City"]
    ).rename(columns={0:'DCID'})

# Get the list of 50 US states by dcid
states = dc.get_property_values(["PlacePagesComparisonStateCohort"], "member")["PlacePagesComparisonStateCohort"]

# Get all the cities in those states by dcid
cities = datacommons.get_places_in(states, "City")

# Convert this list of cities into a dataframe
cities_list = [(key, x) for key,val in cities.items() for x in val]
cities_df = pd.DataFrame(cities_list, columns=['State_DCID', 'City_DCID']) 

# Merge with our original data
cities_cdc500 = pd.merge(cdc500_dcids, cities_df, left_on="DCID", right_on = "City_DCID")
commented

Hi John, you can get the parent place by the property "containedInPlace". In Python, it would be:

dc.get_property_values(["geoId/06"], "containedInPlace", out=True)

Thanks, that's really helpful and it solved the issue!