【问题标题】:Keep original structure of dataframe after groupBy in PySpark在 PySpark 中 groupBy 后保留数据帧的原始结构
【发布时间】:2017-07-10 10:54:32
【问题描述】:

我有一个包含一些社交媒体数据的大型 csv:

message_id, user_id, message, date
"1", "123", "some message blah blah", "Sun May 12 15:08:58 +0000 2013"
"2", "123", "another message blah", "Sun June 12 15:08:58 +0000 2013"
"3", "123", "i want this message removed", "Sun June 12 15:08:58 +0000 2013"
"4", "321", "more blah", "Mon June 12 15:08:58 +0000 2013"

并希望根据组内的某些条件删除消息(对于此示例,组可以是user_id

这就是我所做的:为我的排除标准创建了一个标准函数,基于此方法定义了一个udf,然后将该函数应用于分组数据:

def exclusion_criteria(data_list):
    keep = []
    for d in data_list:
        if some_condition:
            keep.append(d)
    return keep

myUdf = udf(exclusion_criteria, ArrayType(StringType()))

msgsDF = session.read.csv("data.csv", header=False)
filterMsgsDF = msgsDF.groupBy("user_id").agg(collect_list("message")
    .alias("message")).withColumn("message",myUdf("message"))

最后我得到的东西看起来像:

filterMsgsDF.take(1)
[Row(user_id='123', _c2=['some message blah blah', 'another message blah'])]

但问题是我正在删除与每条消息相关的信息(message_iddate)。我最终想要的是这样的

["1", "123", "some message blah blah", "Sun May 12 15:08:58 +0000 2013"]
["2", "123", "another message blah", "Sun June 12 15:08:58 +0000 2013"]
["4", "321", "more blah", "Mon June 12 15:08:58 +0000 2013"]

有没有办法加入这些其他信息或在 groupBy / agg 步骤中保留它?也许groupBy 不是最好的方法吗?

【问题讨论】:

  • 只需在同一列表中附加有关消息的其他信息。即 messageId、message、date 将是单个列表。
  • 对不起,我没有关注。我在哪里附加其他信息?
  • [Row(user_id='123', _c2=[[123,'some message blah blah',date],[124, 'another message blah',date]])]
  • 有点像我上面描述的。如果您仍然不明白,请告诉我,如果这是您要寻找的结果,我会回答。
  • 我明白你在描述什么,但我不知道如何实现。

标签: apache-spark dataframe pyspark


【解决方案1】:

类似:

filterMsgsDF = msgsDF.withColumn('message_list', collect_list(msgsDF['message']).over(Window.partitionBy('user_id')))

输出:

+----------+-------+---------------------------+-------------------------------+---------------------------------------------------------------------------+
|message_id|user_id|message                    |date                           |message_list                                                               |
+----------+-------+---------------------------+-------------------------------+---------------------------------------------------------------------------+
|1         |123    |some message blah blah     |Sun May 12 15:08:58 +0000 2013 |[some message blah blah, another message blah, i want this message removed]|
|2         |123    |another message blah       |Sun June 12 15:08:58 +0000 2013|[some message blah blah, another message blah, i want this message removed]|
|3         |123    |i want this message removed|Sun June 12 15:08:58 +0000 2013|[some message blah blah, another message blah, i want this message removed]|
|4         |321    |more blah                  |Mon June 12 15:08:58 +0000 2013|[more blah]                                                                |
+----------+-------+---------------------------+-------------------------------+---------------------------------------------------------------------------+

【讨论】:

    猜你喜欢
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 2023-02-24
    • 1970-01-01
    • 2018-08-19
    相关资源
    最近更新 更多