Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.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
Apache spark 我怎样才能使一个列对与一个组有关?_Apache Spark_Pyspark_Group By_Apache Spark Sql - Fatal编程技术网

Apache spark 我怎样才能使一个列对与一个组有关?

Apache spark 我怎样才能使一个列对与一个组有关?,apache-spark,pyspark,group-by,apache-spark-sql,Apache Spark,Pyspark,Group By,Apache Spark Sql,我有一个数据帧和一个id列作为一个组。对于每个id,我希望以以下方式对其元素进行配对: title id sal 1 summer 1 fada 1 row 2 winter 2 gole 2 jack 3 noway 3 输出 title id pair sal 1 None summer 1 summer,sal fada

我有一个数据帧和一个
id
列作为一个组。对于每个
id
,我希望以以下方式对其元素进行配对:

title     id
sal        1
summer     1
fada       1
row        2
winter     2
gole       2
jack       3
noway      3
输出

title     id     pair
sal        1      None
summer     1      summer,sal
fada       1      fada,summer
row        2      None
winter     2      winter, row
gole       2      gole,winter
jack       3      None
noway      3      noway,jack
正如您在输出中所看到的,我们将组
id
的最后一个元素与其上方的一个元素配对。由于组中的第一个元素没有一对,我将其放入
None
。我还应该提到,这可以通过以下代码在
pandas
中完成,但我需要
Pyspark
代码,因为我的数据很大

df=data.assign(pair=data.groupby('id')['title'].apply(lambda x: x.str.cat(x.shift(1),sep=',')))

                    |

我不能再强调Spark数据框是无序的行集合了,所以说“上面的元素”之类的东西是未定义的,没有一个列来排序。您可以使用
F.单调地增加id()
来伪造订单,但我不确定这是否是您想要的

from pyspark.sql import functions as F, Window

w = Window.partitionBy('id').orderBy(F.monotonically_increasing_id())

df2 = df.withColumn(
    'pair', 
    F.when(
        F.lag('title').over(w).isNotNull(),
        F.concat_ws(',', 'title', F.lag('title').over(w))
    )
)

df2.show()
+------+---+-----------+
| title| id|       pair|
+------+---+-----------+
|   sal|  1|       null|
|summer|  1| summer,sal|
|  fada|  1|fada,summer|
|  jack|  3|       null|
| noway|  3| noway,jack|
|   row|  2|       null|
|winter|  2| winter,row|
|  gole|  2|gole,winter|
+------+---+-----------+

我不能再强调Spark数据框是无序的行集合了,所以说“上面的元素”之类的东西是未定义的,没有一个列来排序。您可以使用
F.单调地增加id()
来伪造订单,但我不确定这是否是您想要的

from pyspark.sql import functions as F, Window

w = Window.partitionBy('id').orderBy(F.monotonically_increasing_id())

df2 = df.withColumn(
    'pair', 
    F.when(
        F.lag('title').over(w).isNotNull(),
        F.concat_ws(',', 'title', F.lag('title').over(w))
    )
)

df2.show()
+------+---+-----------+
| title| id|       pair|
+------+---+-----------+
|   sal|  1|       null|
|summer|  1| summer,sal|
|  fada|  1|fada,summer|
|  jack|  3|       null|
| noway|  3| noway,jack|
|   row|  2|       null|
|winter|  2| winter,row|
|  gole|  2|gole,winter|
+------+---+-----------+

我无法复制你的解决方案。最终结果中的行顺序错误。是的,行顺序不同,因为问题中没有定义顺序。这真的不是一个很好的问题,但我只是想帮忙。为什么PySpark会弄乱顺序,为什么要使用这个
单调递增的\u id
方法?因为Spark数据帧中没有固有的顺序。数据帧是没有排序的分布式数据结构,由于在窗口函数中的无序排列,它们可能以任何顺序结束。我使用它是因为我在
lag
函数中需要一个order by,但OP的问题中没有任何排序依据。我无法重现您的解决方案。最终结果中的行顺序错误。是的,行顺序不同,因为问题中没有定义顺序。这真的不是一个很好的问题,但我只是想帮忙。为什么PySpark会弄乱顺序,为什么要使用这个
单调递增的\u id
方法?因为Spark数据帧中没有固有的顺序。数据帧是没有排序的分布式数据结构,由于窗口函数中的无序,它们可能以任何顺序结束。我使用它是因为我需要
lag
函数中的order by,但OP的问题中没有任何排序依据。