use country_converter instead of pycountry
This commit is contained in:
parent
dadc372ecd
commit
f4d821ad2a
@ -93,22 +93,16 @@ def prepare_hotmaps_database(regions):
|
|||||||
gdf.rename(columns={"index_right": "bus"}, inplace=True)
|
gdf.rename(columns={"index_right": "bus"}, inplace=True)
|
||||||
gdf["country"] = gdf.bus.str[:2]
|
gdf["country"] = gdf.bus.str[:2]
|
||||||
|
|
||||||
# the .sjoin can lead to duplicates if a geom is in two regions
|
# the .sjoin can lead to duplicates if a geom is in two overlapping regions
|
||||||
if gdf.index.duplicated().any():
|
if gdf.index.duplicated().any():
|
||||||
import pycountry
|
|
||||||
|
|
||||||
# get all duplicated entries
|
# get all duplicated entries
|
||||||
duplicated_i = gdf.index[gdf.index.duplicated()]
|
duplicated_i = gdf.index[gdf.index.duplicated()]
|
||||||
# convert from raw data country name to iso-2-code
|
# convert from raw data country name to iso-2-code
|
||||||
s = df.loc[duplicated_i, "Country"].apply(
|
code = cc.convert(gdf.loc[duplicated_i, "Country"], to="iso2")
|
||||||
lambda x: pycountry.countries.lookup(x).alpha_2
|
# screen out malformed country allocation
|
||||||
)
|
gdf_filtered = gdf.loc[duplicated_i].query("country == @code")
|
||||||
# Get a boolean mask where gdf's country column matches s's values for the same index
|
|
||||||
mask = gdf["country"] == gdf.index.map(s)
|
|
||||||
# Filter gdf using the mask
|
|
||||||
gdf_filtered = gdf[mask]
|
|
||||||
# concat not duplicated and filtered gdf
|
# concat not duplicated and filtered gdf
|
||||||
gdf = pd.concat([gdf.drop(duplicated_i), gdf_filtered]).sort_index()
|
gdf = pd.concat([gdf.drop(duplicated_i), gdf_filtered])
|
||||||
|
|
||||||
return gdf
|
return gdf
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user