Built with Tutify.io

    AIStudyLabs

    Mapping a Python Dict to a Polars Series

    Mapping a Python Dict to a Polars Series

    Mapping a Python dictionary to a Polars Series allows you to replace specific values in the Series based on a key-value relationship defined in the dictionary. This is a useful technique for transforming or cleaning data, particularly when you need to convert categorical values to numerical representations, standardize inconsistent labels, or perform other types of value replacement.

    Mapping a Python Dictionary to a Polars Series refers to the process of replacing specific values in a Polars Series based on a dictionary mapping. In this process, the keys in the dictionary correspond to the values in the Series, and the dictionary values are used to replace the matching Series values. In this article, I will explain how to map a Python dict to a polars series.

    Key Points –

    <a id="h-usage-of-mapping-a-dict-to-a-polars-series"></a>

    Usage of Mapping a Dict to a Polars Series

    Mapping a dictionary to a Polars Series allows you to replace specific values in the Series based on a key-value mapping from the dictionary. It's commonly used to convert categorical data (e.g., string labels) to numeric representations or to update the Series with predefined values.

    To run some examples of mapping a dictionary to a Polars Series, let’s create a Polars Series.

    
    import polars as pl
    
    # Sample Series
    ser = pl.Series(["a", "b", "c", "d"])
    print("Original Series:\n", ser)
    

    Yields below output.

    You can perform basic mapping on a Series using the replace_strict() method, which allows you to replace specific values within the Series. The replace_strict() function performs replacements based on a strict mapping, meaning it only replaces values exactly matching those specified in the map.

    
    # Mapping dictionary
    mapping = {
        "a": 1,
        "b": 2,
        "c": 3,
    }
    
    # Replace using the dictionary
    result = ser.replace_strict(mapping, default=None)
    print("Series After Mapping:\n", result)
    

    Here,

    <a id="h-mapping-string-keys-to-numeric-values"></a>

    Mapping String Keys to Numeric Values

    You can easily map string keys to numeric values in a Polars Series using a dictionary with replace_strict() function. This is a common technique for converting categorical data (such as string labels) into numeric representations, which is often required for tasks like machine learning.

    
    import polars as pl
    
    # Sample Series with string keys
    ser = pl.Series(["apple", "banana", "cherry", "date", "elderberry"])
    
    # Mapping dictionary for string keys to numeric values
    mapping = {
        "apple": 1,
        "banana": 2,
        "cherry": 3,
        "elderberry": 5
    }
    
    # Replace using the dictionary with string keys and numeric values
    result = ser.replace_strict(mapping, default=-1)
    print("Mapped Series (String Keys to Numeric Values):\n", result)
    
    # Output:
    # Mapped Series (String Keys to Numeric Values):
    # shape: (5,)
    # Series: '' [i64]
    # [
    #	1
    #	2
    #	3
    #	-1
    #	5
    # ]
    

    Here,

    <a id="h-mapping-boolean-to-integer"></a>

    Mapping Boolean to Integer

    You can map Boolean values (True/False) to integer values (such as 1 and 0) in a Polars Series using a dictionary with replace_strict().

    
    import polars as pl
    
    # Sample Series with Boolean values
    ser = pl.Series([True, False, True, False, True])
    
    # Mapping dictionary for Boolean to Integer
    mapping = {
        True: 1,
        False: 0
    }
    
    # Replace using the dictionary to map Boolean to Integer
    result = ser.replace_strict(mapping)
    print("Mapped Series (Boolean to Integer):\n", result)
    
    # Output:
    # Mapped Series (Boolean to Integer):
    # shape: (5,)
    # Series: '' [i64]
    # [
    #	1
    #	0
    #	1
    #	0
    #	1
    # ]
    

    Here,

    <a id="h-mapping-with-multiple-keys"></a>

    Mapping with Multiple Keys

    To map multiple keys in a Polars Series with a dictionary, you can directly replace values for each key using the replace_strict() function.

    
    import polars as pl
    
    # Sample Series
    ser = pl.Series(["a", "b", "c", "d", "e", "f"])
    
    # Mapping dictionary with multiple keys
    mapping = {"a": 1,"b": 2,"c": 3,"e": 5,"f": 6,}
    
    # Replace using the dictionary with multiple keys
    result = ser.replace_strict(mapping, default=None)
    print("Mapped Series with Multiple Keys:\n", result)
    
    # Output:
    # Mapped Series with Multiple Keys:
    # shape: (6,)
    # Series: '' [i64]
    # [
    #	1
    #	2
    #	3
    #	null
    #	5
    #	6
    # ]
    

    Here,

    <a id="h-use-replace-strict-for-exact-matches"></a>

    Use replace_strict() for Exact Matches

    replace_strict() in Polars is specifically designed for exact matches. It ensures that only values exactly present in the dictionary keys are replaced; no partial or fuzzy matching is performed.

    
    import polars as pl
    
    # Sample Series
    ser = pl.Series(["Spark", "Panda", "Pandas", "pandas", "Polars", "Spark"])
    
    # Mapping dictionary (case-sensitive)
    mapping = {
        "Spark": "Pyspark",
        "Pandas": "Python"
    }
    
    # Apply replace_strict (case-sensitive exact matching)
    result = ser.replace_strict(mapping, default="Unknown")
    print("Mapped Series (Exact Match):\n", result)
    
    # Output:
    # Mapped Series (Exact Match):
    # shape: (6,)
    # Series: '' [str]
    # [
    #	"Pyspark"
    #	"Unknown"
    #	"Python"
    #	"Unknown"
    #	"Unknown"
    #	"Pyspark"
    # ]
    

    Here,

    <a id="h-replacing-values-in-a-dataframe-column"></a>

    Replacing Values in a DataFrame Column

    You can apply dictionary-based replacement to a specific column in a Polars DataFrame using the with_columns() method along with replace_strict() inside an expression.

    
    import polars as pl
    
    # Sample DataFrame
    df = pl.DataFrame({
        "status": ["yes", "no", "yes", "maybe", "no"]
    })
    
    # Mapping dictionary
    mapping = {
        "yes": 1,
        "no": 0
    }
    
    # Replace values in 'status' column using the mapping
    result = df.with_columns(
        pl.col("status").replace_strict(mapping, default=-1).alias("status_mapped")
    )
    print("Mapped DataFrame:\n", result)
    
    # Output:
    # Mapped DataFrame:
    # shape: (5, 2)
    ┌────────┬───────────────┐
    │ status ┆ status_mapped │
    │ ---    ┆ ---           │
    │ str    ┆ i64           │
    ╞════════╪═══════════════╡
    │ yes    ┆ 1             │
    │ no     ┆ 0             │
    │ yes    ┆ 1             │
    │ maybe  ┆ -1            │
    │ no     ┆ 0             │
    └────────┴───────────────┘
    

    Here,

    <a id="h-conclusion"></a>

    Conclusion

    In conclusion, mapping a dictionary to a Polars Series provides a powerful and efficient way to transform and manipulate data. It allows you to replace specific values in a Series with corresponding values from a dictionary, which is particularly useful for converting categorical data, standardizing values, and preparing datasets for tasks like machine learning.

    Happy Learning!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-reference"></a>

    Reference