Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用SparkSQL将一列作为索引来查找另一列中的单词?_Python_Apache Spark_Pyspark_Apache Spark Sql - Fatal编程技术网

Python 如何使用SparkSQL将一列作为索引来查找另一列中的单词?

Python 如何使用SparkSQL将一列作为索引来查找另一列中的单词?,python,apache-spark,pyspark,apache-spark-sql,Python,Apache Spark,Pyspark,Apache Spark Sql,我的数据帧如下所示: 我想使用top5中的索引列表来查找单词中对应的单词 例如,如果在第一行,words是[I,am,a,student,how,about,you]并且top5是[5,4,0,1,2],那么我想要一个单词形式为words的新列,其索引是top5的数量,因此结果是I,am,a,how,about。 我怎样才能做到呢?我可以用scala提供解决方案。我希望这有帮助 我假设您在名为df的数据帧中有数据 val result = df.rdd // gives you an rdd

我的数据帧如下所示:

我想使用top5中的索引列表来查找单词中对应的单词

例如,如果在第一行,
words
[I,am,a,student,how,about,you]
并且
top5
[5,4,0,1,2]
,那么我想要一个单词形式为
words
的新列,其索引是
top5
的数量,因此结果是
I,am,a,how,about

我怎样才能做到呢?

我可以用scala提供解决方案。我希望这有帮助

我假设您在名为df的数据帧中有数据

val result = df.rdd // gives you an rdd of row 
.map { row =>
        val id = row.getString(0) // first column
        val words = row.getAs[Seq[String]]("words").toArray // second column
        val top5 = row.getAs[Seq[Int]]("top5").toArray // third column

        val requiredValues = new ListBuffer[String]() // to store the result

        top5.foreach(x => requiredValues += words(x)) // extract data for "words5" for ever value in "top5"

        (id,words,top5,requiredValues.toArray)
      }

我可以用scala提供解决方案。我希望这有帮助

我假设您在名为df的数据帧中有数据

val result = df.rdd // gives you an rdd of row 
.map { row =>
        val id = row.getString(0) // first column
        val words = row.getAs[Seq[String]]("words").toArray // second column
        val top5 = row.getAs[Seq[Int]]("top5").toArray // third column

        val requiredValues = new ListBuffer[String]() // to store the result

        top5.foreach(x => requiredValues += words(x)) // extract data for "words5" for ever value in "top5"

        (id,words,top5,requiredValues.toArray)
      }

由于
top5
中的值的数量是固定的,因此可以轻松使用括号表示法或
getItem
。使用问题中的示例:

from pyspark.sql.functions import col, array

df = sc.parallelize([
    (["I", "am", "a", "student", "how", "about", "you"], [5, 4, 0, 1, 2])
]).toDF(["words", "top5"])
您可以:

df.select([col("words")[col("top5")[i]] for i in range(5)])
(由于Spark 3.0中的行为改变而不再适用)或:

两者的结果相同:

+--------------+--------------+--------------+--------------+--------------+
|单词[top5[0]]|单词[top5[1]]|单词[top5[2]]|单词[top5[3]]|单词[top5[4]]|
+--------------+--------------+--------------+--------------+--------------+
|关于|如何|我|是| a|
+--------------+--------------+--------------+--------------+--------------+
如果您想要一个数组列,只需使用
array
函数包装上面的一个列即可:

df.select(array(*[
    col("words").getItem(col("top5")[i]) for i in range(5)
]).alias("top5mapped"))
+----------------------+
|前5名|
+----------------------+
|[关于,如何,我,是,a]|
+----------------------+

由于
top5
中的值的数量是固定的,因此您可以轻松地使用括号表示法或
getItem
。使用问题中的示例:

from pyspark.sql.functions import col, array

df = sc.parallelize([
    (["I", "am", "a", "student", "how", "about", "you"], [5, 4, 0, 1, 2])
]).toDF(["words", "top5"])
您可以:

df.select([col("words")[col("top5")[i]] for i in range(5)])
(由于Spark 3.0中的行为改变而不再适用)或:

两者的结果相同:

+--------------+--------------+--------------+--------------+--------------+
|单词[top5[0]]|单词[top5[1]]|单词[top5[2]]|单词[top5[3]]|单词[top5[4]]|
+--------------+--------------+--------------+--------------+--------------+
|关于|如何|我|是| a|
+--------------+--------------+--------------+--------------+--------------+
如果您想要一个数组列,只需使用
array
函数包装上面的一个列即可:

df.select(array(*[
    col("words").getItem(col("top5")[i]) for i in range(5)
]).alias("top5mapped"))
+----------------------+
|前5名|
+----------------------+
|[关于,如何,我,是,a]|
+----------------------+