I have a small PySpark DataFrame df:
index col1
0 1
1 3
2 4
And a dictionary:
LOOKUP = {0: 2, 1: 5, 2: 5, 3: 4, 4: 6}
I now want to add an extra column col2 to df, equal to the LOOKUP values of col1.
My output should look like this:
index col1 col2
0 1 5
1 3 4
2 4 6
I tried using:
df = df.withColumn(col("col2"), LOOKUP[col("col1")])
But this gave me errors, as well as using expr.
How to achieve this in PySpark?