2

In Pandas, one can do an operation like this:

mapping = {
    'a': 'The letter A',
    'b': 'The letter B',
    'c': 'The third letter'
}

x = pd.Series(['a', 'b', 'a', c']).map(mapping)

and obtain something like

pd.Series([
    'The letter A',
    'The letter B',
    'The letter A',
    'The third letter'
])

Naively, I can achieve this in a PySpark DataFrame with something like

import pyspark.sql.functions as F
import pyspark.sql.functions as T

def _map_values_str(value, mapping, default=None):
    """ Apply a mapping, assuming the result is a string """
    return mapping.get(value, default)

map_values_str = F.udf(_map_values_str, T.StringType())

mapping = {
    'a': 'The letter A',
    'b': 'The letter B',
    'c': 'The third letter'
}

data = spark.createDataFrame([('a',), ('b',), ('a',), ('c',)], schema=['letters'])
data = data.withColumn('letters_mapped', map_values_str(F.col('letters'), mapping))

But UDFs like this tend to be somewhat slow on large data sets in my experience. Is there a more efficient way?

1 Answer 1

3

I think in this case you could convert the dict to a DataFrame and simply use a join:

import pyspark.sql.functions as F

mapping = {
    'a': 'The letter A',
    'b': 'The letter B',
    'c': 'The third letter'
}
# Convert so Spark DataFrame
mapping_df = spark.sparkContext.parallelize([(k,)+(v,) for k,v in mapping.items()]).toDF(['letters','val'])

data = spark.createDataFrame([('a',), ('b',), ('a',), ('c',)], schema=['letters'])
data = data.join(mapping_df.withColumnRenamed('val','letters_mapped'),'letters','left')
data.show()

Output:

+-------+----------------+
|letters|  letters_mapped|
+-------+----------------+
|      c|The third letter|
|      b|    The letter B|
|      a|    The letter A|
|      a|    The letter A|
+-------+----------------+

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.