title: How to pass schema to rdd dynamically in pyspark
body: <p>I have a dataframe that consists of text column and doing some operations on that. I am converting the dataframe to rdd and doing other operation but when I am converting rdd back to pyspark dataframe, it is expecting a schema. I have passed the schema as well but it is not working.</p>
  <p>Below is the dataframe:</p>
  <pre><code>temp = spark.createDataFrame([
      (0, &quot;Julia is awesome&quot;),
      (1, &quot;I wish Java-DL could use case-classes!@#$&quot;),
      (2, &quot;Data-science is  cool&quot;),
      (3, &quot;Machine&quot;)
  ], [&quot;id&quot;, &quot;words&quot;])
  </code></pre>
  <p>Below is the schema:</p>
  <pre><code>print(temp.schema)
  
  StructType(List(StructField(id,LongType,true),StructField(words,StringType,true)))
  </code></pre>
  <p>Performing a regex transformation on words column</p>
  <pre><code>temp_1 = temp.withColumn('words', F.regexp_replace('words', r'(\s)+', '$1'))
  </code></pre>
  <p>I am converting the pyspark dataframe to rdd below</p>
  <pre><code>rdd_df = temp_1.rdd.map(lambda x: ' '.join(x['words']))
  </code></pre>
  <p>Converting the rdd back to spark dataframe</p>
  <pre><code>result_df = spark.createDataFrame(rdd_df, schema = temp.schema)
  </code></pre>
  <p>Error:</p>
  <pre><code>TypeError: StructType can not accept object 'J u l i a   i s   a w e s o m e' in type &lt;class 'str'&gt;
  </code></pre>