如何从每列的列中提取特定元素?

时间:2017-11-22 15:16:05

标签: scala apache-spark spark-dataframe

我在Spark 2.2.0和Scala 2.11.8中有以下DataFrame。

+----------+-------------------------------+
|item      |        other_items            |
+----------+-------------------------------+
|  111     |[[444,1.0],[333,0.5],[666,0.4]]|
|  222     |[[444,1.0],[333,0.5]]          |
|  333     |[]                             |
|  444     |[[111,2.0],[555,0.5],[777,0.2]]|

我想获得以下DataFrame:

+----------+-------------+
|item      | other_items |
+----------+-------------+
|  111     | 444         |
|  222     | 444         |
|  444     | 111         |

所以,基本上,我需要从item中为每一行提取第一个other_items。另外,我需要忽略那些在[]中有空数组other_products的行。

我该怎么做?

我试过这种方法,但它没有给我预期的结果。

result = df.withColumn("other_items",$"other_items"(0))

printScheme提供以下输出:

 |-- item: string (nullable = true)
 |-- other_items: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- _1: string (nullable = true)
 |    |    |-- _2: double (nullable = true)

0 个答案:

没有答案