【发布时间】:2020-10-28 02:24:16
【问题描述】:
我有这样的数据:
+----------+----------+--------+
| Location | Product | Amount |
+----------+----------+--------+
| London | Fish | 307 |
| London | Chips | 291 |
| London | Beer | 147 |
| Paris | Baguettes| 217 |
| Paris | Cheese | 103 |
| Paris | Champagne| 74 |
+----------+----------+--------+
自然地,每个位置都有许多位置和许多产品。我想以这样的数据框结束:
+----------+---------------------+-------------------------+-------+-------------------------+
| Location | Most Common Product | 2nd Most Common Product |..... | Nth Most Common Product |
+----------+---------------------+-------------------------+-------+-------------------------+
| London | Fish | Chips | .... | something |
| Paris | Baguettes | Cheese | .... | something else |
+----------+---------------------+-------------------------+-------+-------------------------+
我想出了最常见的方法,使用 this。
在将其扩展到最常见的 N 个时,我可以创建另一个删除了这些行的数据框,再次运行该过程以获得第二个最常见的,然后按位置将它们连接在一起。使用适当的列命名,可以将其放入循环中运行 N 次,每次迭代添加一列。
但是,这将非常缓慢,因为它会在每次迭代中进行分区和加入。例如,我怎样才能以更好的方式获得每个位置最常见的 50 个?
【问题讨论】:
标签: python sql database pyspark