title: Remove spaces from all columns using spark
body: <p>I have Dataframe with some columns:</p>
  <pre><code>+------+-------------+------+---------------+--------------+
  |CustId|         Name|Salary|          State|       Country|
  +------+-------------+------+---------------+--------------+
  |     1|   Brad Eason|   100|New South Wales|     Australia|
  |     2|Tracy Hopkins|   200|        England|United Kingdom|
  |     3|   Todd Boyes|   300|        England|United Kingdom|
  |     4|     Roy Phan|   400|      Minnesota| United States|
  |     5|  Harold Ryan|   500|     Washington| United States|
  +------+-------------+------+---------------+--------------+
  </code></pre>
  <p>To replace all the <code>space</code> of a string column with <code>_</code>, I have made the following changes:</p>
  <ul>
  <li>get all the string type columns to avoid exception while performing String operation.</li>
  <li>foreach string type column replace <code>SPACE</code> with <code>_</code>.</li>
  </ul>
  <pre class="lang-scala prettyprint-override"><code>import org.apache.spark.sql.types.StringType
  
  val trimColumns=customers.schema.fields.filter(_.dataType.isInstanceOf[StringType])
  val arrayOfDf = trimColumns.map(f=&gt;{
      customers.withColumn(f.name,regexp_replace(col(f.name), &quot; &quot;, &quot;_&quot;))
  })
  </code></pre>
  <p>The above code results in an array of dataframes which have valid data of string column in each element.</p>
  <pre><code>scala&gt; arrayOfDf(1).select(&quot;Name&quot;).show(4)
  +-------------+
  |         Name|
  +-------------+
  |   Brad_Eason|
  |Tracy_Hopkins|
  |   Todd_Boyes|
  |     Roy_Phan|
  +-------------+
  </code></pre>
  <p>Now I need to pick the first columns from the first element, second columns from the second element of the array, and so on...</p>
  <p>Is there any better way for this approach?</p>