0

I need to append one column to another and have written a function to perform the same:

def concat_content(input_df, left_column, right_columns):
    for col_to_change in right_columns:
        print(col_to_change)
        input_df = input_df.withColumn(F.col(col_to_change), F.concat(F.col(left_column), F.lit(" | "),F.coalesce(F.col(col_to_change), F.lit("None"))))

    return input_df

new_final = concat_content(final, "name_txt", ["group_txt", "sub_group_txt"])

but I am getting error:

TypeError: Column is not iterable

What can I try to resolve this?

halfer
  • 19,824
  • 17
  • 99
  • 186
user3222101
  • 1,270
  • 2
  • 24
  • 43
  • `withColumn` need string as a first argument, column name. So just don't wrap your first argument, `col_to_change` in F.col, and you should be fine. – Rayan Ral May 20 '20 at 17:18
  • Does this answer your question? [Concatenate columns in Apache Spark DataFrame](https://stackoverflow.com/questions/31450846/concatenate-columns-in-apache-spark-dataframe) – Ani Menon May 20 '20 at 18:11
  • thank you Rayan, sorted! – user3222101 May 20 '20 at 19:34

1 Answers1

1

Try this

def concat_content(input_df, left_column, right_columns):
    for col_to_change in right_columns:
        print(col_to_change)
        input_df = input_df.withColumn(col_to_change, F.concat(F.col(left_column), F.lit(" | "),F.coalesce(F.col(col_to_change), F.lit("None"))))

    return input_df

new_final = concat_content(final, "name_txt", ["group_txt", "sub_group_txt"])

With column takes string as first argument not column.

Shubham Jain
  • 5,327
  • 2
  • 15
  • 38