title: How to encode string values into numeric values in Spark DataFrame
body: <p>I have a DataFrame with two columns:</p>
  
  <pre><code>df = 
    Col1   Col2
    aaa    bbb
    ccc    aaa
  </code></pre>
  
  <p>I want to encode String values into numeric values. I managed to do it in this way:</p>
  
  <pre><code>import org.apache.spark.ml.feature.{OneHotEncoder, StringIndexer}
  
  val indexer1 = new StringIndexer()
                      .setInputCol("Col1")
                      .setOutputCol("Col1Index")
                      .fit(df)
  
  val indexer2 = new StringIndexer()
                      .setInputCol("Col2")
                      .setOutputCol("Col2Index")
                      .fit(df)
  
  val indexed1 = indexer1.transform(df)
  val indexed2 = indexer2.transform(df)
  
  val encoder1 = new OneHotEncoder()
                      .setInputCol("Col1Index")
                      .setOutputCol("Col1Vec")
  
  val encoder2 = new OneHotEncoder()
                      .setInputCol("Col2Index")
                      .setOutputCol("Col2Vec")
  
  val encoded1 = encoder1.transform(indexed1)
  encoded1.show()
  
  val encoded2 = encoder2.transform(indexed2)
  encoded2.show()
  </code></pre>
  
  <p>The problem is that <code>aaa</code> is encoded in different ways in two columns.
  How can I encode my DataFrame in order to get the new one correctly encoded, e.g.:</p>
  
  <pre><code>df_encoded = 
     Col1   Col2
     1      2
     3      1
  </code></pre>