给出如下表格:
+--+------------------+-----------+
|id| diagnosis_age| diagnosis|
+--+------------------+-----------+
| 1|2.1843037179180302| 315.320000|
| 1| 2.80033330216659| 315.320000|
| 1| 2.8222365762732| 315.320000|
| 1| 5.64822705794013| 325.320000|
| 1| 5.686557787521759| 335.320000|
| 2| 5.70572315231258| 315.320000|
| 2| 5.724888517103389| 315.320000|
| 3| 5.744053881894209| 315.320000|
| 3|5.7604813374292005| 315.320000|
| 3| 5.77993740687426| 315.320000|
+--+------------------+-----------+
我想通过选择最常诊断的诊断和年龄,将每个id减少到一行。
输出如下所示:
+--+------------------+-----------+
|id| diagnosis_age| diagnosis|
+--+------------------+-----------+
| 1|2.1843037179180302| 315.320000|
| 2| 5.70572315231258| 315.320000|
| 3| 5.744053881894209| 315.320000|
+--+------------------+-----------+
我试过这样的方法:
SELECT id, diagnosis, age,
COUNT(distinct diagnosis) OVER (partition by id)
FROM table
但我真的什么也做不到!
使用以下代码:
wc=Window().partitionBy("id", "diagnosis").orderBy("diagnosis_age")
wc2=Window().partitionBy("id")
ddfc.withColumn("count", F.count("diagnosis").over(wc))\
.withColumn("max", F.max("count").over(wc2))\
.filter("count=max")\
.groupBy("id").agg(F.first("diagnosis_age").alias("diagnosis_age"), F.first("diagnosis").alias("diagnosis"))\
.orderBy("id")\
.groupBy("diagnosis")\
.count()\
.orderBy("count", ascending = False)\
.show(5)
我得到:
+--------------+-----+
|diagnosis |count|
+--------------+-----+
| V20.2|22179|
| 382.900000|12985|
| 389.900000|11333|
| 381.810000| 7448|
| 493.900000| 3249|
+--------------+-----+
鉴于您的代码(修改为按诊断分组并获得5个最常见的诊断)输出:
+--------------+------+
|diagnosis |count |
+--------------+------+
| 389.900000 |13268 |
| 382.900000 | 7572 |
| V20.2 | 6193 |
| 381.810000 | 4735 |
| V72.19 | 4115 |
+--------------+------+
谢谢你的帮助。
1条答案
按热度按时间a11xaf1n1#
在统计学中,这是最常见的值,称为模式。
您可以使用聚合和
row_number()
: