PySpark - 按列值拆分/过滤 DataFrame

5 python dataframe apache-spark apache-spark-sql pyspark

我有一个类似于此示例的 DataFrame:

Timestamp | Word | Count

30/12/2015 | example_1 | 3

29/12/2015 | example_2 | 1

28/12/2015 | example_2 | 9

27/12/2015 | example_3 | 7

... | ... | ...
Run Code Online (Sandbox Code Playgroud)

并且我想通过“word”列的值拆分此数据框以获得 DataFrame 的“列表”(以在下一步中绘制一些数字)。例如:

DF1

Timestamp | Word | Count

30/12/2015 | example_1 | 3
Run Code Online (Sandbox Code Playgroud)

DF2

Timestamp | Word | Count

29/12/2015 | example_2 | 1

28/12/2015 | example_2 | 9
Run Code Online (Sandbox Code Playgroud)

DF3

Timestamp | Word | Count

27/12/2015 | example_3 | 7
Run Code Online (Sandbox Code Playgroud)

有没有办法用 PySpark (1.6) 做到这一点?

zer*_*323 5

它不会有效率,但您可以在唯一值列表上使用过滤器进行映射:

words = df.select("Word").distinct().flatMap(lambda x: x).collect()
dfs = [df.where(df["Word"] == word) for word in words]
Run Code Online (Sandbox Code Playgroud)

后火花2.0

words = df.select("Word").distinct().rdd.flatMap(lambda x: x).collect()
Run Code Online (Sandbox Code Playgroud)