<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: TypeError: Column is not iterable when using more than one columns in withColumn() in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/typeerror-column-is-not-iterable-when-using-more-than-one/m-p/151061#M53570</link>
    <description>&lt;P&gt;Hi, This is a super old question but answering in case anyone else comes across it.&lt;/P&gt;
&lt;P&gt;This isn't working because the add months expects an integer rather than a column name you can get round this by using expr() inside withColumn:&lt;/P&gt;
&lt;P&gt;from pyspark.sql.functions import expr&lt;/P&gt;
&lt;P&gt;df = df.withColumn(&lt;BR /&gt;"history_effective_quarter",&lt;BR /&gt;expr("add_months(history_effective_month, -(month(history_effective_month) % 3) + 1)")&lt;BR /&gt;)&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;&lt;BR /&gt;Emma&lt;/P&gt;</description>
    <pubDate>Mon, 16 Mar 2026 15:43:56 GMT</pubDate>
    <dc:creator>emma_s</dc:creator>
    <dc:date>2026-03-16T15:43:56Z</dc:date>
    <item>
      <title>TypeError: Column is not iterable when using more than one columns in withColumn()</title>
      <link>https://community.databricks.com/t5/data-engineering/typeerror-column-is-not-iterable-when-using-more-than-one/m-p/28973#M20730</link>
      <description>&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am trying to find quarter start date from a date column. I get the expected result when i write it using selectExpr() but when i add the same logic in .withColumn() i get TypeError: Column is not iterable&lt;/P&gt;&lt;P&gt;I am using a workaround as follows &lt;/P&gt; workaround:- df=df.selectExpr('*',"date_sub(history_effective_date,dayofmonth(history_effective_date)-1) as history_effective_month")&lt;P&gt;&lt;/P&gt; &lt;P&gt;selectExpr:- df.selectExpr("add_months(history_effective_month,-(month(history_effective_month)%3)+1)","history_effective_month").show(5)&lt;/P&gt; &lt;P&gt;output-&lt;/P&gt; &lt;P&gt;+----------+-----------------------+ | qtr|history_effective_month| +----------+-----------------------+ |2017-07-01| 2017-06-01| |2016-04-01| 2016-05-01| |2015-10-01| 2015-09-01| |2012-01-01| 2012-01-01| |2012-01-01| 2012-01-01| +----------+-----------------------+&lt;/P&gt; &lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;PRE&gt;&lt;CODE&gt;&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;/CODE&gt;&lt;/PRE&gt;withColumn():-&lt;P&gt;&lt;/P&gt; &lt;P&gt;df.withColumn("history_effective_quarter",add_months('history_effective_month',-(month('history_effective_month')%3)+1))&lt;/P&gt; &lt;P&gt;&lt;/P&gt; &lt;P&gt;TypeError Traceback (most recent call last) &amp;lt;ipython-input-259-0bb78d27d2a7&amp;gt; in &amp;lt;module&amp;gt;() 1 #pricerxDF.selectExpr("add_months(history_effective_month,-(month(history_effective_month)%3)+1) as qtr","history_effective_month").show(5) ----&amp;gt; 2 pricerxDF.withColumn("history_effective_quarter",add_months('history_effective_month',-(month('history_effective_month')%3)+1))&lt;/P&gt; &lt;P&gt;~/anaconda3/lib/python3.6/site-packages/pyspark/sql/functions.py in add_months(start, months) 968 """ 969 sc = SparkContext._active_spark_context --&amp;gt; 970 return Column(sc._jvm.functions.add_months(_to_java_column(start), months)) 971 972 &lt;/P&gt; &lt;P&gt;~/anaconda3/lib/python3.6/site-packages/py4j/java_gateway.py in &lt;B&gt;call&lt;/B&gt;(self, *args) 1122 1123 def &lt;B&gt;call&lt;/B&gt;(self, *args): -&amp;gt; 1124 args_command, temp_args = self._build_args(*args) 1125 1126 command = proto.CALL_COMMAND_NAME +\&lt;/P&gt; &lt;P&gt;~/anaconda3/lib/python3.6/site-packages/py4j/java_gateway.py in _build_args(self, *args) 1086 def _build_args(self, *args): 1087 if self.converters is not None and len(self.converters) &amp;gt; 0: -&amp;gt; 1088 (new_args, temp_args) = self._get_args(args) 1089 else: 1090 new_args = args&lt;/P&gt; &lt;P&gt;~/anaconda3/lib/python3.6/site-packages/py4j/java_gateway.py in _get_args(self, args) 1073 for converter in self.gateway_client.converters: 1074 if converter.can_convert(arg): -&amp;gt; 1075 temp_arg = converter.convert(arg, self.gateway_client) 1076 temp_args.append(temp_arg) 1077 new_args.append(temp_arg)&lt;/P&gt; &lt;P&gt;~/anaconda3/lib/python3.6/site-packages/py4j/java_collections.py in convert(self, object, gateway_client) 498 ArrayList = JavaClass("java.util.ArrayList", gateway_client) 499 java_list = ArrayList() --&amp;gt; 500 for element in object: 501 java_list.add(element) 502 return java_list&lt;/P&gt; &lt;P&gt;~/anaconda3/lib/python3.6/site-packages/pyspark/sql/column.py in &lt;B&gt;iter&lt;/B&gt;(self) 248 249 def &lt;B&gt;iter&lt;/B&gt;(self): --&amp;gt; 250 raise TypeError("Column is not iterable") 251 252 # string methods&lt;/P&gt; &lt;P&gt;TypeError: Column is not iterable &lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 03 Dec 2017 11:19:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/typeerror-column-is-not-iterable-when-using-more-than-one/m-p/28973#M20730</guid>
      <dc:creator>PrasadGaikwad</dc:creator>
      <dc:date>2017-12-03T11:19:34Z</dc:date>
    </item>
    <item>
      <title>Re: TypeError: Column is not iterable when using more than one columns in withColumn()</title>
      <link>https://community.databricks.com/t5/data-engineering/typeerror-column-is-not-iterable-when-using-more-than-one/m-p/151061#M53570</link>
      <description>&lt;P&gt;Hi, This is a super old question but answering in case anyone else comes across it.&lt;/P&gt;
&lt;P&gt;This isn't working because the add months expects an integer rather than a column name you can get round this by using expr() inside withColumn:&lt;/P&gt;
&lt;P&gt;from pyspark.sql.functions import expr&lt;/P&gt;
&lt;P&gt;df = df.withColumn(&lt;BR /&gt;"history_effective_quarter",&lt;BR /&gt;expr("add_months(history_effective_month, -(month(history_effective_month) % 3) + 1)")&lt;BR /&gt;)&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;&lt;BR /&gt;Emma&lt;/P&gt;</description>
      <pubDate>Mon, 16 Mar 2026 15:43:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/typeerror-column-is-not-iterable-when-using-more-than-one/m-p/151061#M53570</guid>
      <dc:creator>emma_s</dc:creator>
      <dc:date>2026-03-16T15:43:56Z</dc:date>
    </item>
  </channel>
</rss>

