df1 =
|---------------------|------------------|------------------|
ID_Machine | Event_Duration | Timestamp |
---|---|---|
1 | 34 | 213 |
--------------------- | ------------------ | ------------------ |
1 | 97 | 572 |
--------------------- | ------------------ | ------------------ |
1 | 78 | 872 |
--------------------- | ------------------ | ------------------ |
2 | 83 | 345 |
--------------------- | ------------------ | ------------------ |
2 | 14 | 718 |
--------------------- | ------------------ | ------------------ |
2 | 115 | 884 |
--------------------- | ------------------ | ------------------ |
首先创建一个名为max的数据框
df_max = df1.groupBy("ID_Machine").agg(F.max("Event_Duration").alias("Event_Duration"))
df_max.show()
你将得到以下数据框:
±---------±-------------+
|ID_Machine|Event_Duration|
±---------±-------------+
| 1| 97|
| 2| 115|
±---------±-------------+
然后通过两个相似的命名列将数据框连接起来,然后重命名事件持续时间:
df_combined = df_max.join(df1, ["ID_Machine", "Event_Duration"]) \
.withColumnRenamed("Event_Duration", "Max_Event_Duration")
df_combined.show()
你将得到想要的结果:
±---------±-----------------±--------+
|ID_Machine|Max_Event_Duration|Timestamp|
±---------±-----------------±--------+
| 2| 115| 884|
| 1| 97| 572|
±---------±-----------------±--------+