我有帖子資料框架和作者資料框架
我需要計算每一天的帖子作者的favCount posts_columns = ["postId"/span>, "authorId"/span>, "date"/span>]
posts = [("1"/span>, "1"/span>, "2020-10-10"/span>)。
("2", "2", "2020-10-10") 。
("3"/span>, "2"/span>, "2020-10-10")。
("4"/span>, "2"/span>, "2020-10-11")
("5"/span>, "3"/span>, "2020-10-11")
("6"/span>, "3"/span>, "2020-10-11") ]
authors_columns = ["authorId"/span>, "favCount"/span>]
作者 = [
("1"/span>, "5"/span>)。
("2", "3") 。
("3", "12") ]
在(內部)連接Post和Authors資料框后,我得到了這個
在(post.authorId=author.authorId)
---------- -------- ---------------
|日期|authorId|favCount|
---------- -------- ---------------
|2020-10-10| 1| 5|
|2020-10-10| 2| 3|
|2020-10-10| 2| 3|
|2020-10-11| 2| 3|
|2020-10-11| 3| 12|
|2020-10-11| 3| 12|
---------- -------- ---------------
現在我想計算每一天作者的favCount之和,最終結果應該是這樣的
---------- -------------
|日期|sum(favCount)|
---------- -------------
|2020-10-10| 8|
|2020-10-11| 15|
---------- -------------
10月10日,我有兩個作者(1和2),共有8個favCount(5 3)
。在10月11日,我有兩個作者(2和3),總共有15個贊(3 12)
。P.S: 我不想計算重復的作者的favCount,每個作者的favCount應該在每天只計算一次
。P.S(2): 我正在使用PySpark和Dataframes,但我不介意用Pandas或甚至SQL來回答
。uj5u.com熱心網友回復:
考慮將df1作為Post和df2作為Authors的資料框架
result = df1.merge(df2, how= 'inner').drop_duplicates(subset=['date', 'authorId'] )
final = result.groupby([result.date])['favCount']。sum()
uj5u.com熱心網友回復:
如果你想試試spark,你可以試試這個
scala代碼:df1.join(df2, Seq("authorId"), "inner") 。 groupBy("date", "authorId").sum()
或者python:
df1.join(df2, ["authorId"], "inner")。 groupBy("date", "authorId")。sum()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/307659.html
標籤:
