In Synapse Notebook using pyspark inbuilt functions how to convert country code to country name as description

2025-01-17T09:13:38.3733333+00:00

Hi Team,

Could you please help us that how to change country code to country name (as description) for a column in synapse notebook using pyspark (modules or libraries) could you please share the code.

we have used below but it is not working.
Eg:

import country_converter as coco
from pyspark.sql.types import StringType
from pyspark.sql.functions import udf, lit,col

def country_converter(country):
    if country:
        return coco.convert(country, to='IS02', not_found=None)
    return None

cc_udf = udf(country_converter, StringType())
Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
5,157 questions
Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
2,318 questions
{count} votes

Accepted answer
  1. Smaran Thoomu 19,390 Reputation points Microsoft Vendor
    2025-01-17T13:32:59.16+00:00

    Hi @SaiSekhar, MahasivaRavi (Philadelphia)

    It seems like the error you are encountering, ModuleNotFoundError: No module named 'country_converter', indicates that the country_converter library is not available in the Synapse environment where you're trying to write the data as a Delta file.To resolve this issue, you'll need to ensure that the country_converter library is installed and accessible within your Spark pool or environment.

    • In Azure Synapse Analytics, you need to install the country_converter library on your Spark pool.
    • The error you're seeing suggests that the module is missing, so we need to install it in the environment where the notebook is being executed.
    • If country_converter is not available in the default packages, download the library's .whl file or .tar.gz from PyPI.
    • In Synapse Studio, go to Manage > Apache Spark Pools > Packages and upload the .whl or .tar.gz file as per below. User's image
    • Attach the library to your Spark pool and restart the pool.
    • Use the following test code to verify if the country_converter module is available: User's image
    • Once the library is installed and the Spark pool is restarted, try executing the notebook again. The error should no longer appear when you're trying to write the data as Delta.

    If the issue persists, please share the specifics of your Synapse environment (such as version or custom configurations), and I can assist you further.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.