<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Select dataframe columns from a sequence of string in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/select-dataframe-columns-from-a-sequence-of-string/m-p/29935#M21626</link>
    <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I also had the same problem, and here's how to make it work using column type and varargs:&lt;/P&gt;// make example dataframe import org.apache.spark.sql.DataFrame val df: DataFrame = sc.parallelize(Seq((1, 2, 3), (4, 5, 6), (7, 8, 9))).toDF("a", "b", "c")
&lt;P&gt;&lt;/P&gt; 
&lt;P&gt;// desired list of column names in string (making it possible programmatically) val column_names_str = Seq[String]("a", "b")&lt;/P&gt; 
&lt;P&gt;// construct list of column names in column type import org.apache.spark.sql.functions.col val column_names_col = column_names_str.map(name =&amp;gt; col(name)) //val column_names_col = column_names_str.map(name =&amp;gt; col(name).as(s"renamed_$name")) // rename if needed&lt;/P&gt; 
&lt;P&gt;// select specific columns from dataframe using varargs syntax &lt;I&gt;* val df_new = df.select(column_names_col: &lt;/I&gt;*) df_new.show() &lt;/P&gt;
&lt;P&gt;This should yield as expected:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;+---+---+
|  a|  b|
+---+---+
|  1|  2|
|  4|  5|
|  7|  8|
+---+---+&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 03 Dec 2015 02:54:33 GMT</pubDate>
    <dc:creator>JongKim</dc:creator>
    <dc:date>2015-12-03T02:54:33Z</dc:date>
    <item>
      <title>Select dataframe columns from a sequence of string</title>
      <link>https://community.databricks.com/t5/data-engineering/select-dataframe-columns-from-a-sequence-of-string/m-p/29934#M21625</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Is there a simple way to select columns from a dataframe with a sequence of string?&lt;/P&gt;
&lt;P&gt;Something like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;val colNames = Seq("c1", "c2")
df.select(colNames)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Nov 2015 21:10:14 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/select-dataframe-columns-from-a-sequence-of-string/m-p/29934#M21625</guid>
      <dc:creator>Jean-FrancoisRa</dc:creator>
      <dc:date>2015-11-25T21:10:14Z</dc:date>
    </item>
    <item>
      <title>Re: Select dataframe columns from a sequence of string</title>
      <link>https://community.databricks.com/t5/data-engineering/select-dataframe-columns-from-a-sequence-of-string/m-p/29935#M21626</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I also had the same problem, and here's how to make it work using column type and varargs:&lt;/P&gt;// make example dataframe import org.apache.spark.sql.DataFrame val df: DataFrame = sc.parallelize(Seq((1, 2, 3), (4, 5, 6), (7, 8, 9))).toDF("a", "b", "c")
&lt;P&gt;&lt;/P&gt; 
&lt;P&gt;// desired list of column names in string (making it possible programmatically) val column_names_str = Seq[String]("a", "b")&lt;/P&gt; 
&lt;P&gt;// construct list of column names in column type import org.apache.spark.sql.functions.col val column_names_col = column_names_str.map(name =&amp;gt; col(name)) //val column_names_col = column_names_str.map(name =&amp;gt; col(name).as(s"renamed_$name")) // rename if needed&lt;/P&gt; 
&lt;P&gt;// select specific columns from dataframe using varargs syntax &lt;I&gt;* val df_new = df.select(column_names_col: &lt;/I&gt;*) df_new.show() &lt;/P&gt;
&lt;P&gt;This should yield as expected:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;+---+---+
|  a|  b|
+---+---+
|  1|  2|
|  4|  5|
|  7|  8|
+---+---+&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Dec 2015 02:54:33 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/select-dataframe-columns-from-a-sequence-of-string/m-p/29935#M21626</guid>
      <dc:creator>JongKim</dc:creator>
      <dc:date>2015-12-03T02:54:33Z</dc:date>
    </item>
    <item>
      <title>Re: Select dataframe columns from a sequence of string</title>
      <link>https://community.databricks.com/t5/data-engineering/select-dataframe-columns-from-a-sequence-of-string/m-p/29936#M21627</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Thanks. I needed to modify the final lines.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;val df_new = df.select(column_names_col:_*)
df_new.show()&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Edward&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Oct 2016 20:21:41 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/select-dataframe-columns-from-a-sequence-of-string/m-p/29936#M21627</guid>
      <dc:creator>vEdwardpc</dc:creator>
      <dc:date>2016-10-01T20:21:41Z</dc:date>
    </item>
  </channel>
</rss>

