我的數(shù)據(jù)框看起來像這樣------+-------+ |cat_id|counter|+------+-------+| 12| 61060|| 1| 542118|| 13| 164700|| 3| 406622|| 5| 54902|| 10| 118281|| 11| 13658|| 14| 72229|| 2| 131206|+------+-------+獲取上述數(shù)據(jù)框的查詢是: grouped_data = dataframe.groupBy("cat_id").agg(count("*").alias("counter"))現(xiàn)在我需要讀取不同 cat_id 的值以將其保存在另一個數(shù)據(jù)庫中。我完成它的方法是在我的 id 上使用 for 循環(huán)for cat_id in cat_ids_map: statsCount = grouped_data.select("counter").filter("cat_id = " + cat_id).collect()[0].counter但我認(rèn)為可以有更好的方法來讀取計數(shù)器而無需 for 循環(huán)。任何的意見都將會有幫助?。。?
如何從 python 中的 Spark 數(shù)據(jù)幀訪問特定列?
幕布斯6054654
2023-05-16 14:26:03