<jupyter_start><jupyter_text>Enrich Variable Name LookupEnrich variable names can be represented using a variety of string representations depending on where you are looking. Frequently there is a need to cross reference between these. The `Country.enrich_variables` property provides the ability to easily retrieve a Pandas data frame. This data frame can be used to perform cross reference lookups, but there also is a hidden method you can use as well, `Country._ba_cntry.get_enrich_variables_from_name_list`. This method accepts any iterabe of strings, and will look up the values by checking for a sequence of case insensitive matches in the `name`, `alias`, `field_name` and `enrich_field_name` columns.NOTE: This requires at least version 1.9.1 of the Python API. This was included with the Pro 2.9 release, but if you have a previous release of Pro, you can get the most recent release of the Python API by [cloning your default Python environment, setting your new environment to be the default](https://pro.arcgis.com/en/pro-app/latest/arcpy/get-started/work-with-python-environments.htm) and [updating the ArcGIS Python API (arcgis) package](https://pro.arcgis.com/en/pro-app/latest/arcpy/get-started/work-with-python-packages.htm).<jupyter_code>from pathlib import Path

from arcgis.geoenrichment import Country
from arcgis.gis import GIS
import pandas as pd<jupyter_output><empty_output><jupyter_text>Load Excel Table with Various Representations of Enrich NamesThis is a table of name variations for enrich variables I was passed to use as an example of different variations needing to be looked up.<jupyter_code>dir_prj = Path.cwd().parent
dir_ref = dir_prj/'references'
xlsx_pth = dir_ref/'arcmap_pro_variables.xlsx'

assert xlsx_pth.exists()
xlsx_df = pd.read_excel(xlsx_pth)
drop_cols = [c for c in xlsx_df.columns if c.startswith('Unnamed:')]
xlsx_df.drop(columns=drop_cols, inplace=True)
xlsx_df.columns = ['guided_workflow_names', 'pro_enrich_names', 'fieldnames_arcmap', 'fieldnames_pro']

xlsx_df<jupyter_output><empty_output><jupyter_text>Create a Country Object InstanceCreate a Country object instance referencing the country you are looking for variables in.<jupyter_code>usa = Country('usa')<jupyter_output><empty_output><jupyter_text>Perform LookupsNow, we just need to provide an interable of strings to be matched into the `get_enrich_variables_from_list` function.  ArcGIS Pro Enrich NamesIf using a list of names used to enrich with ArcGIS Pro, you can easily look up these values.<jupyter_code>print(list(xlsx_df.pro_enrich_names))
usa._ba_cntry.get_enrich_variables_from_name_list(xlsx_df.pro_enrich_names)<jupyter_output><empty_output><jupyter_text>ArcMap Enrich NamesArcMap required a slightly different string format. Hence, if migrating from ArcMap, the same workflow can be used to retrieve these values as well.<jupyter_code>print(list(xlsx_df.fieldnames_arcmap))
usa._ba_cntry.get_enrich_variables_from_name_list(xlsx_df.fieldnames_arcmap)<jupyter_output><empty_output><jupyter_text>Field NamesOnce enriched, the `enrich_name` string has the period replaced for the output attibute field name (`enrich_field_name`). We can use also use these output field name values to lookup relevant rows in the `enrich_variables` table.<jupyter_code>print(list(xlsx_df.fieldnames_pro))
usa._ba_cntry.get_enrich_variables_from_name_list(xlsx_df.fieldnames_pro)<jupyter_output><empty_output><jupyter_text>NOTE: Another way to get this list is simply using a list comprehension listing the field names using `arcpy.ListFields`.```import arcpyfrom arcgis.gis import Countrypth_to_fc = r'C:/to/somwhere/somedb.fdgb/fc_of_stuff'field_name_list = [f.name for f in arcpy.ListFields(pth_to_fc)]usa = Country('usa')usa._ba_cntry.get_enrich_variables_from_name_list(field_name_list)```  Preprocessing with LookupThe first column we are saving for last. These values do not have explicit matches. However, using pandas, we can split the string at the period and only keep the first part. This first part can then be looked up since it matches the `name` column.<jupyter_code>xlsx_df.guided_workflow_names.head()<jupyter_output><empty_output><jupyter_text>We do it slightly differently here. Instead of simply creating an iterable to use for lookup, in this case, we add the `search_names` to the existing dataframe generated from the Excel table we read in.<jupyter_code>xlsx_df['search_names'] = xlsx_df.guided_workflow_names.str.split('.').apply(lambda val: val[0])

xlsx_df.head()<jupyter_output><empty_output><jupyter_text>Since the function just needs an `Iterable` object, we can use the column (Pandas Series) we just created to look up valid values.<jupyter_code>enrich_vars = usa._ba_cntry.get_enrich_variables_from_name_list(xlsx_df.search_names)

enrich_vars<jupyter_output><empty_output><jupyter_text>Concantenating for Local EnrichThe `arcpy.ba.EnrichLayer` tool in ArcGIS Pro needs a semicolon separated string of `enrich_name` values for input. From any of the above results, this can quickly be created from the `enich_name` column.<jupyter_code>vars_str = ';'.join(enrich_vars.enrich_name.values)

print(vars_str)<jupyter_output>Health.MEDHINC_CY;ACS_Housing_Summary_rep.MOETOTPOP;travelMPI.TADULTBASE;Age_by_Sex_by_Race_Profile_rep.TOTPOP_CY;Policy.DIVINDX_CY;householdincome.ACSMEDHINC;householdincome.MOEMEDHINC;householdincome.RELMEDHINC;Community_Profile_rep.X11001_I;ElectronicsInternet.MP09147a_B;ElectronicsInternet.MP09147a_I;ElectronicsInternet.MP09148a_B;ElectronicsInternet.MP09148a_I;ElectronicsInternet.MP09134a_B;ElectronicsInternet.MP09134a_I;education.X11001_X;education.X11001_A;HealthPersonalCare.MP14129a_B;industry.INDAGRI_CY;industry.INDMIN_CY;industry.INDCONS_CY;householdsbyraceofhouseholder.HHWHTHHR10;householdsbyraceofhouseholder.HHBLKHHR10;householdsbyraceofhouseholder.HHAIHHR10;householdsbyraceofhouseholder.HHASNHHR10;householdsbyraceofhouseholder.HHPIHHR10;householdsbyraceofhouseholder.HHOTHHHR10;KeyUSFacts.POPGRW10CY;tapestryadultsNEW.TADULT01;tapestryadultsNEW.TADULT02;population.ACSTOTPOP;population.RELTOTPOP;populationtotals.HHPOP_CY;populationtotals.FAMPOP_CY<jupyter_text>Adding Onto Existing TableAlso, if we want, we can add the values needed for the Enrich Layer tool onto our existing table.<jupyter_code>add_df = enrich_vars[['name', 'enrich_name']].set_index('name')

add_df.head()
joined_df = xlsx_df.join(add_df, on='search_names')

joined_df.info()
joined_df.head()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 34 entries, 0 to 33
Data columns (total 6 columns):
 #   Column                 Non-Null Count  Dtype 
---  ------                 --------------  ----- 
 0   guided_workflow_names  34 non-null     object
 1   pro_enrich_names       34 non-null     object
 2   fieldnames_arcmap      34 non-null     object
 3   fieldnames_pro         34 non-null     object
 4   search_names           34 non-null     object
 5   enrich_name            34 non-null     object
dtypes: object(6)
memory usage: 1.7+ KB<jupyter_text>Finally, if all we want is the input values, and the enrich names, we can quickly filter to just these columns.<jupyter_code>vals_df = joined_df.loc[:,['guided_workflow_names', 'enrich_name']]

vals_df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07823691460055096, 'fraction_numerical': 0.013911845730027548, 'mean_word_length': 6.416751787538304, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 48, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '68590', 'license': 'Apache-2.0', 'n_tokens_mistral': 2509, 'n_tokens_neox': 2358, 'n_words': 628, 'path': 'notebooks/enrich-variable-name-lookup.ipynb', 'repo_name': 'knu2xs/business-analyst-python-api-examples', 'size': '7260'}