我的數據如下。 It has three attributes: location, date, and student_id.PySpark中的等價值groupyby()。unique()
在熊貓,我可以做
GROUPBY([ '位置', '日期'])[ 'student_id數據']。唯一的()
看到的每個位置,在不同的日期,哪些學生同時去那裏學習。
我的問題是如何在PySpark中提取相同的信息?謝謝。
我的數據如下。 It has three attributes: location, date, and student_id.PySpark中的等價值groupyby()。unique()
在熊貓,我可以做
GROUPBY([ '位置', '日期'])[ 'student_id數據']。唯一的()
看到的每個位置,在不同的日期,哪些學生同時去那裏學習。
我的問題是如何在PySpark中提取相同的信息?謝謝。
可以使用collect_set在pyspark把它完成,
df.groupby('location','date').agg(F.collect_set('student_id')).show()
+--------+----------+-----------------------+
|location| date|collect_set(student_id)|
+--------+----------+-----------------------+
| 18250|2015-01-04| [347416]|
| 18253|2015-01-02| [167633, 188734]|
| 18250|2015-01-03| [363796]|
+--------+----------+-----------------------+
假設你的數據在表格的行:
(location, date, student_id)
你可以這樣做:
data
.map(lambda row: (row[0:2], {row[2]})
.reduceByKey(lambda a, b: a.union(b))
.collect()