PYSPARK : casting string to float when reading a csv file
<p>I'm reading a csv file to dataframe</p>

<pre><code>datafram = spark.read.csv(fileName, header=True)
</code></pre>

<p>but the data type in datafram  is String, I want to change data type to float. Is there any way to do this efficiently?</p>

<p>The most straightforward way to achieve this is by casting.</p>

<pre><code>dataframe = dataframe.withColumn("float", col("column").cast("double"))
</code></pre>

python apache-spark pyspark