13

I have the following data and would like to get the result with a text prefix:

Input dataframe:

sk            id       
2306220722    117738

Current code:

df.withColumn("Remarks", concat_ws("MCA", col("ID")))

Expected output:

sk           id      Remarks  
2306220722   117738  MCA 117738

I would like to prefix the id column with "MCA" and add the resulting string to the Remarks column.

2 Answers 2

19

Simply use the concat command in combination with lit. lit will take a value and produce a column with only this value, it can be a string, double, etc.

val df2 = df.withColumn("Remarks", concat(lit("MCA "), col("id")))

Using the example dataframe in the question and running df2.show() gives

+----------+------+----------+
|        sk|    id|   Remarks|
+----------+------+----------+
|2306220722|117738|MCA 117738|
+----------+------+----------+
Sign up to request clarification or add additional context in comments.

1 Comment

If you have null values in your column, stackoverflow.com/questions/58310246/…
0

Dataset data1= data.withColumn("Name", functions.concat(functions.lit("SAR "),functions.col("Name")) ).show();

+---------+------------+---------+
|     Name|STRING_VALUE|NUM_VALUE|
+---------+------------+---------+
|SAR name2|      value2|        2|
|SAR name1|      value1|        1|
|SAR name1|      value1|        1|
|SAR name2|      value2|        2|
+---------+------------+---------+

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.