Pyspark Pypark中的Groupby
我有一个约490亿条记录的数据帧。数据框看起来像-Pyspark Pypark中的Groupby,pyspark,pyspark-sql,Pyspark,Pyspark Sql,我有一个约490亿条记录的数据帧。数据框看起来像- id transaction_no amount 1 321 100 1 100 50 1 32 200 2 54 50 2 20
id transaction_no amount
1 321 100
1 100 50
1 32 200
2 54 50
2 20 1000
3 41 44
4 78 400
4 65 200
id count amount
1 3 350
2 2 1050
3 1 44
4 2 600
我的最终输出看起来像-
id transaction_no amount
1 321 100
1 100 50
1 32 200
2 54 50
2 20 1000
3 41 44
4 78 400
4 65 200
id count amount
1 3 350
2 2 1050
3 1 44
4 2 600
我可以用python来做,但是如何用pyspark来做呢
import pyspark.sql.functions as f
df=spark.createDataFrame([(1,321,100),(1,100,50),(1,32,200),(2,54,50),(2,20,1000),(3,41,44),(4,78,400),(4,65,200)],['id','transaction_no','amount'])
df_req = df.groupby('id').agg(f.sum('amount').alias('amount'),f.count('id').alias('count'))
df_req.show()
+---+------+-----+
| id|amount|count|
+---+------+-----+
| 1| 350| 3|
| 3| 44| 1|
| 2| 1050| 2|
| 4| 600| 2|
+---+------+-----+