title: Calculate Mode of each Column of dataframe, Store it in a list and make a dataframe using the it
body: <p>I am  finding the 'MODE' of all columns in my dataframe and storing them in a list.
  The code that is calculating my MODE of each column :</p>
  
  <pre><code>from pyspark.sql.functions import *
   #calculating mode value
  mode_val = []
  for i in df_num.columns :
         cnts = df_num.groupBy(i).count()
         mode = cnts.join(
         cnts.agg(max("count").alias("max_")), col("count") == col("max_")
          ).limit(1)
          mode2 = mode.withColumn(i,col(i).cast("double"))
          mode_val.append(mode2.first()[0])
  </code></pre>
  
  <h1>output</h1>
  
  <p>[6500.0,
   0.0,
   None,
   1300.0,
   3.0,
   3.0,
   0.0,
   None,
   38000.0]</p>
  
  <p>When i am trying to convert the list into a dataframe i seem to get an error.</p>
  
  <p>This is my code for converting my list of MODE to dataframe :</p>
  
  <pre><code>univar_df4 = spark.createDataFrame(mode_val,["Mode"])
  </code></pre>
  
  <h1>error</h1>
  
  <pre><code>---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-171-d5ca3ecf8d79&gt; in &lt;module&gt;()
        1 #not able to apply to dataframe.
  ----&gt; 2 univar_df4 = spark.createDataFrame(mode_val,["Mode"])
  
  /usr/lib/spark/python/pyspark/sql/session.py in createDataFrame(self, data, schema, samplingRatio, verifySchema)
      535             rdd, schema = self._createFromRDD(data.map(prepare), schema, samplingRatio)
      536         else:
  --&gt; 537             rdd, schema = self._createFromLocal(map(prepare, data), schema)
      538         jrdd = self._jvm.SerDeUtil.toJavaArray(rdd._to_java_object_rdd())
      539         jdf = self._jsparkSession.applySchemaToPythonRDD(jrdd.rdd(), schema.json())
  
  /usr/lib/spark/python/pyspark/sql/session.py in _createFromLocal(self, data, schema)
      399 
      400         if schema is None or isinstance(schema, (list, tuple)):
  --&gt; 401             struct = self._inferSchemaFromList(data)
      402             converter = _create_converter(struct)
      403             data = map(converter, data)
  
  /usr/lib/spark/python/pyspark/sql/session.py in _inferSchemaFromList(self, data)
      331             warnings.warn("inferring schema from dict is deprecated,"
      332                           "please use pyspark.sql.Row instead")
  --&gt; 333         schema = reduce(_merge_type, map(_infer_schema, data))
      334         if _has_nulltype(schema):
      335             raise ValueError("Some of types cannot be determined after inferring")
  
  /usr/lib/spark/python/pyspark/sql/types.py in _infer_schema(row)
      990 
      991     else:
  --&gt; 992         raise TypeError("Can not infer schema for type: %s" % type(row))
      993 
      994     fields = [StructField(k, _infer_type(v), True) for k, v in items]
  
  TypeError: Can not infer schema for type: &lt;class 'float'&gt;
  </code></pre>