2
    from pyspark.sql import Row, functions as F
    row = Row("UK_1","UK_2","Date","Cat")
    df = (sc.parallelize
    ([
        row(1,1,'12/10/2016',"A"),
        row(1,2,None,'A'),
        row(2,1,'14/10/2016','B'),
        row(3,3,'!~2016/2/276','B'),
        row(None,1,'26/09/2016','A'),
        row(1,1,'12/10/2016',"A"),
        row(1,2,None,'A'),
        row(2,1,'14/10/2016','B'),
        row(None,None,'!~2016/2/276','B'),
        row(None,1,'26/09/2016','A')
        ]).toDF())

       pks = ["UK_1","UK_2"]

      df1 = (
      df
      .select(columns) 
       #.withColumn('pk',F.concat(pks))
      .withColumn('pk',F.concat("UK_1","UK_2"))
      )

   df1.show()

Is there a way I can pass in a list of columns into the concat? I want to use the code for scenarios where the columns can be varying and i would like to pass it as a list.

1 Answer 1

5

Yes, the syntax is *args (variable number of arguments) in python:

df.withColumn("pk", F.concat(*pks)).show()

+----+----+------------+---+----+
|UK_1|UK_2|        Date|Cat|  pk|
+----+----+------------+---+----+
|   1|   1|  12/10/2016|  A|  11|
|   1|   2|        null|  A|  12|
|   2|   1|  14/10/2016|  B|  21|  
|   3|   3|!~2016/2/276|  B|  33|
|null|   1|  26/09/2016|  A|null|
|   1|   1|  12/10/2016|  A|  11|
|   1|   2|        null|  A|  12|
|   2|   1|  14/10/2016|  B|  21|
|null|null|!~2016/2/276|  B|null|
|null|   1|  26/09/2016|  A|null|
+----+----+------------+---+----+
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting the following error ,AnalysisException: u'cannot resolve \'"UK_1"\' given input columns: [UK_1, UK_2, Date, Cat];'
It seems you have some extra quotes around column names. You can check pks, and make sure the strings don't have unnecessary quotes.

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.