{"id":15836,"date":"2018-11-14T14:19:32","date_gmt":"2018-11-14T22:19:32","guid":{"rendered":"https:\/\/devwww.3cloudsolutions.com\/post\/python-in-power-bi-geocode-using-azure-maps-webinar-preview-2\/"},"modified":"2024-04-17T08:59:54","modified_gmt":"2024-04-17T15:59:54","slug":"python-in-power-bi-geocode-using-azure-maps-webinar-preview","status":"publish","type":"post","link":"https:\/\/3cloudsolutions.com\/resources\/python-in-power-bi-geocode-using-azure-maps-webinar-preview\/","title":{"rendered":"Python in Power BI &#8211; Geocode using Azure Maps (Webinar Preview)"},"content":{"rendered":"<p>Many maps in Power BI require latitude and longitude coordinates to display geography, but many data sources only contain place names. Geocoding is the process of obtaining latitude and longitude for a given location. Geocoding with Power BI is nothing new, but it may never have been as simple. In addition to several\u00a0<a href=\"https:\/\/dataveld.com\/2015\/08\/24\/geocoding-in-a-pinch-power-query-and-the-google-maps-api\/\" target=\"_blank\" rel=\"noopener\">existing solutions<\/a> that you could use to get coordinates for your data, the new Python script in Power BI Desktop is now an option. As a benefit, you do not need to manually construct web requests or parse JSON responses. In fact, it only takes one line of Python code.<\/p>\n<p><img decoding=\"async\" style=\"width: 805px; display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/3cloudsolutions.com\/wp-content\/uploads\/2022\/11\/Python-in-Power-BI-1.png\" alt=\"Python in Power BI\" width=\"805\" \/><\/p>\n<p>To see geocoding and more solutions using Python for Power BI, be sure to attend BlueGranite\u2019s webinar\u00a0<strong>Python in Power BI: Navigate the Possibilities<\/strong>.<\/p>\n<p><!--more--><\/p>\n<p><strong>Date:<\/strong> Dec. 6, 2018<br \/>\n<strong>Time:<\/strong> 11 a.m. EST<br \/>\n<strong>Presenter:<\/strong> David Eldersveld, Senior Consultant and Microsoft MVP<br \/>\n<strong style=\"background-color: transparent;\">Register here: <\/strong><a href=\"\/python-in-power-bi-webinar-dec-2018\" target=\"_blank\" rel=\"noopener\">Python in Power BI Webinar<\/a><\/p>\n<h3><strong>Sample: Geocode Location Data for Power BI using Python and Azure Maps<\/strong><\/h3>\n<p>Python makes translating location names to latitude and longitude easy by offering connections to numerous geocoding providers. One of these services is Microsoft&#8217;s own Azure Maps. All you need is a simple Python script to specify a column, choose the provider, and supply the provider&#8217;s access key.<\/p>\n<p>While the following is not a complete step-by-step process, please join the webinar on Dec. 6th to see this demo live.<\/p>\n<ul>\n<li>First, obtain a key for the Azure Maps service using the <a href=\"https:\/\/portal.azure.com\" target=\"_blank\" rel=\"noopener\">Azure Portal<\/a>. Sign up for the Azure Maps service within the Portal if needed.<\/li>\n<\/ul>\n<ul>\n<li>In your Python environment, install both the <strong><a href=\"http:\/\/geopandas.org\" target=\"_blank\" rel=\"noopener\">GeoPandas <\/a><\/strong>and <strong><a href=\"https:\/\/geopy.readthedocs.io\/en\/stable\/\" target=\"_blank\" rel=\"noopener\">GeoPy\u00a0<\/a><\/strong>libraries. For Python, I use the Anaconda distribution available from <a href=\"https:\/\/www.anaconda.com\/download\">https:\/\/www.anaconda.com\/download<\/a>. The following <em>conda<\/em> commands were executed to install the libraries using the <strong>Anaconda Prompt<\/strong>. If you are not using Anaconda, you can install using <em>pip<\/em>\u00a0instead of <em>conda\u00a0<\/em>from your command line.<\/li>\n<\/ul>\n<p><img decoding=\"async\" style=\"width: 600px; display: block; margin: 0px auto;\" src=\"https:\/\/3cloudsolutions.com\/wp-content\/uploads\/2022\/11\/pbi-geocode-python-geopandas-install.png\" alt=\"pbi-geocode-python-geopandas-install\" width=\"600\" \/><\/p>\n<p><img decoding=\"async\" style=\"width: 600px; display: block; margin: 0px auto;\" src=\"https:\/\/3cloudsolutions.com\/wp-content\/uploads\/2022\/11\/pbi-geocode-python-geopy-install.png\" alt=\"pbi-geocode-python-geopy-install\" width=\"600\" \/><\/p>\n<ul>\n<li>Import your location data into Power BI. If needed, use Power Query to prepare the data so that a single column contains the address or other type of location that you need to geocode. In the sample, I create a simple column named <strong>Location<\/strong> that combines separate <strong>City<\/strong>\u00a0and <strong>State<\/strong> columns.\u00a0While this type of data preparation can be done in Python too, I want to focus on a simplified Python script that only handles the geocoding.<\/li>\n<\/ul>\n<p><img decoding=\"async\" style=\"width: 600px; display: block; margin: 0px auto;\" src=\"https:\/\/3cloudsolutions.com\/wp-content\/uploads\/2022\/11\/pbi-geocode-python-location-column.png\" alt=\"pbi-geocode-python-location-column\" width=\"600\" \/><\/p>\n<ul>\n<li>Add the Python code. The <em>Transform<\/em> tab in Power Query has the option to <strong>Run Python script<\/strong>. This adds the corresponding\u00a0<strong>Python.Execute\u00a0<\/strong>function to the underlying code. While the original DataFrame (named\u00a0<em>dataset<\/em> by default)\u00a0contains a single <strong>Location\u00a0<\/strong>field,\u00a0Azure Maps provides two new fields in response: <strong>Address<\/strong> and <strong>Geometry<\/strong>. The <em>geopandas.tools.geocode <\/em>function only needs three arguments: the original &#8220;Location&#8221; field, the service provider (Azure Maps), and the key.\u00a0The Python code combines the Azure Maps response with the original data in the same line by adding &#8220;Address&#8221; and &#8220;Geometry&#8221; as new columns within the\u00a0<em>dataset<\/em> DataFrame.<\/li>\n<\/ul>\n<p><span style=\"background-color: transparent;\"><img decoding=\"async\" style=\"width: 600px; display: block; margin: 0px auto;\" src=\"https:\/\/3cloudsolutions.com\/wp-content\/uploads\/2022\/11\/pbi-geocode-python-run-python-script-1.png\" alt=\"pbi-geocode-python-run-python-script-1\" width=\"600\" \/>\u00a0<\/span><\/p>\n<ul>\n<li>Parse the Geometry field to split out separate Latitude and Longitude columns. This is another series of steps that could be done in the Python script itself, but which Power Query is perfectly capable to handle as well.<\/li>\n<\/ul>\n<p><img decoding=\"async\" style=\"width: 600px; margin: 0px auto; display: block;\" src=\"https:\/\/3cloudsolutions.com\/wp-content\/uploads\/2022\/11\/pbi-geocode-python-geocoded-columns.png\" alt=\"pbi-geocode-python-geocoded-columns\" width=\"600\" \/><\/p>\n<p><img decoding=\"async\" style=\"width: 600px; display: block; margin: 0px auto;\" src=\"https:\/\/3cloudsolutions.com\/wp-content\/uploads\/2022\/11\/pbi-geocode-python-geocode-final.png\" alt=\"pbi-geocode-python-geocode-final\" width=\"600\" \/><\/p>\n<p>The final Power Query script includes a parameter named <em>AzureMapsSubscriptionKey<\/em> in place of the initially hardcoded Azure Maps key. Ignoring the imports, uniting Python with Azure Maps only takes <strong>one line of Python code\u00a0<\/strong>embedded within your Power Query M script!<\/p>\n<pre>#\"Run Python script\" = Python.Execute(\"import geopandas#(lf)import geopy#(lf)#(lf)dataset[['Address','Geometry']] = geopandas.tools.geocode(dataset.Location, provider='azure', subscription_key='\" &amp;Text.From(AzureMapsSubscriptionKey)&amp; \"')#(lf)\",[dataset=#\"Added Custom\"])<\/pre>\n<p><span style=\"background-color: transparent;\">Happy coding! Once again, if you would like to learn more uses for Python in Power BI, attend BlueGranite\u2019s free webinar on Dec. 6<\/span><span style=\"background-color: transparent;\">.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Attend BlueGranite\\&#8217;s free Python in Power BI webinar on December 6, 2018. In addition to easily obtaining latitude and longitude using Python and Azure Maps, many more examples will be demoed.<\/p>\n","protected":false},"author":21,"featured_media":14162,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":""},"categories":[260,378],"tags":[273],"class_list":["post-15836","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-ai","category-past-webinars","tag-power-bi","topics-blog"],"acf":[],"_links":{"self":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts\/15836","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/comments?post=15836"}],"version-history":[{"count":0,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts\/15836\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/media\/14162"}],"wp:attachment":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/media?parent=15836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/categories?post=15836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/tags?post=15836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}