Replacing whitespace in all column names in spark Dataframe : <p>I have spark dataframe with whitespaces in some of column names, which has to be replaced with underscore.</p>

<p>I know a single column can be renamed using <code>withColumnRenamed()</code> in sparkSQL, but to rename 'n' number of columns, this function has to chained 'n' times (to my knowledge).</p>

<p>To automate this, i have tried:</p>

<pre><code>val old_names = df.columns()        // contains array of old column names

val new_names = old_names.map { x =&gt; 
   if(x.contains(" ") == true) 
      x.replaceAll("\\s","_") 
   else x 
}                    // array of new column names with removed whitespace.
</code></pre>

<p>Now, how to replace df's header with <code>new_names</code></p>
