1

I have List[N] like below

val check = List ("a","b","c","d")

where N can be any number of elements.

I have a dataframe with only column called "value". Based on the contents of value i need to create N columns with column names as elements in the list and column contents as substring(x,y)

I have tried all possible ways, like withColumn, selectExpr, nothing works. Please consider substring(X,Y) where X and Y as some numbers based on some metadata

Below are my different codes which I tried, but none worked,


val df = sqlContext.read.text("xxxxx")
val coder: (String => String) = (arg: String) => {
val param = "NULL"
if (arg.length() > Y )
arg.substring(X,Y)
else
val sqlfunc = udf(coder)
val check = List ("a","b","c","d")
for (name <- check){val testDF2 = df.withColumn(name, sqlfunc(df("value")))}

testDF2 has only last column d and other columns such as a,b,c are not added in table


var z:Array[String] = new Array[String](check.size)
var i=0
for ( x <- check ) {
if ( (i+1) == check.size) {
z(i) = s""""substring(a.value,X,Y) as $x""""
i = i+1}
else{
z(i) = s""""substring(a.value,X,Y) as $x","""
i = i+1}}
val zz = z.mkString(" ")
df.alias("a").selectExpr(s"$zz").show()

This throws error


Please help how to add columns in DF dynamically with column names as elements in List

I am expecting an Df like below

-----------------------------
Value| a | b | c | d | .... N
-----------------------------
|xxx|xxx|xxx|xxx|xxx|xxxxxx-                
|xxx|xxx|xxx|xxx|xxx|xxxxxx- 
|xxx|xxx|xxx|xxx|xxx|xxxxxx-
-----------------------------
4
  • please format your question and show example data plus expected ouptut. Commented Mar 13, 2017 at 10:29
  • still unclear, what is in the original df Commented Mar 13, 2017 at 10:35
  • just one column named value with some data in it @mtoto Commented Mar 13, 2017 at 10:38
  • then please share it, and again clean up your code. I tried to do it for you but its still a mess. Commented Mar 13, 2017 at 10:40

1 Answer 1

4

you can dynamically add columns from your list using for instance this answer by user6910411 to a similar question (see her/his full answer for more possibilities):

val newDF = check.foldLeft(<yourdf>)((df, name) => df.withColumn(name,<yourUDF>$"value"))

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.