title: AWS Glue Python transformation
body: <p>I am making a simple transformation with AWS Glue where I added a customised code to calculate the basic rgb from rgb table with rgb data, comparing each rgb with the static array in code.</p>
  <p>I am moving from dynamicframe to dataframe to perform the transformation, I'm adding an iteration where I add new column to the dataframe with each calculated value.</p>
  <p>The issue happens when I try to move from dataframe to dynamicFrame again. The error is:</p>
  <pre class="lang-py prettyprint-override"><code>Py4JJavaError: An error occurred while calling z:com.amazonaws.services.glue.DynamicFrame.apply. java.lang.StackOverflowError
  </code></pre>
  <p>This error happens exactly on this line:</p>
  <pre class="lang-py prettyprint-override"><code>dyf = DynamicFrame.fromDF(df, glueContext, &quot;data1&quot;)
  </code></pre>
  <h1>Code</h1>
  <pre class="lang-py prettyprint-override"><code>def getColor (glueContext, dfc) -&gt; DynamicFrameCollection:
      
      import math
      import pyspark.sql.functions as f
      
      colors = [
      (&quot;black&quot;, (0, 0, 0)),
      (&quot;silver&quot;, (192, 192, 192)),
      (&quot;gray&quot;, (128, 128, 128)),
      (&quot;white&quot;, (255, 255, 255)),
      (&quot;maroon&quot;, (128, 0, 0)),
      (&quot;red&quot;, (255, 0, 0)),
      (&quot;purple&quot;, (128, 0, 128)),
      (&quot;fuchsia&quot;, (255, 0, 255)),
      (&quot;green&quot;, (0, 128, 0)),
      (&quot;lime&quot;, (0, 255, 0)),
      (&quot;olive&quot;, (128, 128, 0)),
      (&quot;yellow&quot;, (255, 255, 0)),
      (&quot;navy&quot;, (0, 0, 128)),
      (&quot;blue&quot;, (0, 0, 255)),
      (&quot;teal&quot;, (0, 128, 128)),
      (&quot;aqua&quot;, (0, 255, 255))
      ]
      
      def distance(a,b):
          dx = a[0]-b[0]
          dy = a[1]-b[1]
          dz = a[2]-b[2]
          #print(&quot;coordenadas:&quot;,dx,dy,dz)
          
          return math.sqrt(dx*dx+dy*dy+dz*dz)
      
      def findclosest(c):
          mn = 999999
          for name,rgb in colors:
             d = distance(c, rgb)
             #print(&quot;valor distance:&quot;, d)
             if d &lt; mn:
                 mn = d
                 color = name
          return color
      
      df = dfc.select(list(dfc.keys())[0]).toDF()
      print(&quot;Valor df:&quot;,df)
      
      # add column dataframe
      #df = df.withColumn(&quot;color_basic&quot;, f.lit(&quot;&quot;))
      #print(&quot;Valor df - column added:&quot;,df)
      
      # retrieving all the elements
      # of the dataframe using collect()
      # Storing in the variable
      data_collect = df.collect()
       
      # looping thorough each row of the dataframe
      for row in data_collect:
          # while looping through each
          #row printing the data of Id, Name and City
          #print(row[&quot;data_colors_red&quot;],&quot; &quot;,row[&quot;data_colors_green&quot;],&quot; &quot;,row[&quot;data_colors_blue&quot;])
          r = float(row[&quot;data_colors_red&quot;])
          g = float(row[&quot;data_colors_green&quot;])
          b = float(row[&quot;data_colors_blue&quot;])
          mid = int(row[&quot;mediaId&quot;])
      
          c = (&quot;colorbasic&quot;,(r,g,b))
          colorb = findclosest(c[1])
          df = df.withColumn(&quot;color_basic&quot;, f.lit(colorb))
          #df = df.select(&quot;data_colors_blue&quot;,&quot;data_colors_green&quot;,&quot;data_colors_red&quot;,&quot;data_colors_pixelFraction&quot;,&quot;data_colors_score&quot;,&quot;mediaId&quot;,f.when(f.col(&quot;mediaId&quot;)==mid, colorb).otherwise( &quot;Not Known&quot; ).alias(&quot;color_basic&quot;))
          
      print(&quot;Valor df - after foreach:&quot;,df)
      
      dyf = DynamicFrame.fromDF(df, glueContext, &quot;data1&quot;)
      
      print(&quot;DynamicFrame:&quot;,dyf)
      
      return(DynamicFrameCollection({&quot;CustomTransform0&quot;: dyf}, glueContext))
  </code></pre>
  <p>Can anyone help me???</p>
  <p>Thanks in advance!
  R.</p>