I got stucked with a data transformation task in pyspark. I want to replace all values of one column in a df with key-value-pairs specified in a dictionary.
dict = {'A':1, 'B':2, 'C':3}
My df looks like this:
+-----------++-----------+
| col1|| col2|
+-----------++-----------+
| B|| A|
| A|| A|
| A|| A|
| C|| B|
| A|| A|
+-----------++-----------+
Now I want to replace all values of col1 by the key-values pairs defined in dict.
Desired Output:
+-----------++-----------+
| col1|| col2|
+-----------++-----------+
| 2|| A|
| 1|| A|
| 1|| A|
| 3|| B|
| 1|| A|
+-----------++-----------+
I tried
df.na.replace(dict, 1).show()
but that also replaces the values on col2, which shall stay untouched.
Thank you for your help. Greetings :)