You can't sub in the descriptions initially, but a simple replacement is quite easy. I have a little function I use in scripts where I want the descriptions instead of the codes.
def domain_descriptions(df, service):
"""
Takes a queried dataframe and its parent service and applies all fieldname aliases and domain names.
Not especially useful for updating web layers, but *great* for when the queried dataframe will be exported to some other format.
Parameters:
df (DataFrame): A dataframe queried out of a hosted feature service or table.
service (FeatureLayer or Table): A layer or table from a Feature Service.
Returns:
None, just modifies the input DataFrame.
"""
aliases = {f'{i["name"]}':f'{i["alias"]}' for (i) in service.properties['fields']}
domains = {i['name']:{str(j['code']):j['name'] for (j) in i['domain']['codedValues']} for (i) in service.properties["fields"] if 'domain' in i and i['domain'] is not None}
for d in domains:
if d in df.columns.to_list():
df[d] = df[d].astype('str').replace(domains[d])
df.rename(columns=aliases, inplace=True)
Try it out! In practice, it might look like this:
fl = gis.content.get('some-itemid').layers[0]
df = fl.query(as_df=True)
domain_descriptions(df, fl)
- Josh Carlson
Kendall County GIS