I have a dataframe that has below columns
field1 , field2 , field3 , field_name
sample data
"a1", "b1", "c1", "field1"
"a2", "b2", "c2", "field2"
"a3", "b3", "c3", "field3"
I want to add new column"fieldvalue" to the data frame, so that it contains value in the column that corresponds to the content of the column "fieldname"
so the first row above will have fieldvalue = "a1", since fieldname contains "field1"
the output data frame should look like
field1, field2, field3 , fieldname, fieldvalue
data
"a1", "b1", "c1", "field1", "a1"
"a2", "b2", "c2", "field2", "b2"
"a3", "b3", "c3", "field3", "c3"
I tried to use below syntax
df1 = df1.withColumn("fieldValue", func.col(func.col("fieldName")))
But it fails with below error, since func.col expects a column , and not a string
Method col([class org.apache.spark.sql.Column]) does not exist