这是我的数据:
| ID | Date |
| 1 | 20-Mar|
| 1 | 30-Mar|
| 1 | 20-Apr|
| 2 | 10-Mar|
| 2 | 12-Mar|
| 3 | 20-Mar|
| 4 | 20-Mar|
| 4 | 9-Mar |
我想按ID分组并创建新列,这些列的日期如下:
| ID | Date_1 | Date_2 | Date_3 | Date_4 |
| 1 | 20-Mar | 30-Mar | 20-Apr |
| 2 | 10-Mar | 12-Mar |
| 3 | 20-Mar |
| 4 | 9-Mar | 20-Mar |
谢谢
4条答案
按热度按时间fcipmucu1#
你可以这样试试
ttcibm8c2#
在sql中,可以使用
row_number()
和条件聚合:xpszyzbs3#
使用
groupBy, collect_list
函数然后基于数组索引值创建日期列。Example:
```sample dataframe
df.show()
+---+------+
| ID| Date|
+---+------+
| 1|20-Mar|
| 1|30-Mar|
| 1|20-Apr|
| 2|10-Mar|
+---+------+
from pyspark.sql.functions import *
df.groupBy("id").agg(collect_list(col("Date")).alias("tmp")).
withColumn("Date_1",col("tmp")[0]).
withColumn("Date_2",col("tmp")[1]).
withColumn("Date_3",col("tmp")[2]).
withColumn("Date_4",col("tmp")[3]).
drop("tmp").
show(10,False)
+---+------+------+------+------+
|id |Date_1|Date_2|Date_3|Date_4|
+---+------+------+------+------+
|1 |20-Mar|30-Mar|20-Apr|null |
|2 |10-Mar|null |null |null |
+---+------+------+------+------+
df.groupBy("id").agg(collect_list(col("Date")).alias("tmp")).
withColumn("Date_1",element_at(col("tmp"),1)).
withColumn("Date_2",element_at(col("tmp"),2)).
withColumn("Date_3",element_at(col("tmp"),3)).
withColumn("Date_4",element_at(col("tmp"),4)).
drop("tmp").
show(10,False)
`Dynamic way:`
df1=df.groupBy(col("id")).agg(collect_list(col("date")))
get the max size of array
size=df.groupBy("id").agg(collect_list(col("Date")).alias("tmp")).select(max(size("tmp"))).collect()[0][0]
df1.select([df1.id]+ [df1.tmp[i].alias("date_"+ str(i+1)) for i in range(size+1)]).
show()
+---+------+------+------+------+
| id|date_1|date_2|date_3|date_4|
+---+------+------+------+------+
| 1|20-Mar|30-Mar|20-Apr| null|
| 2|10-Mar| null| null| null|
+---+------+------+------+------+
wqlqzqxt4#
加载Dataframe
分组依据
ID
将日期收集为数组DateArr
运行选择ID
使用动态添加列map
(在python+scala中提供)如果您想使您的代码更独立于列的数量,那么请选中
Solution 2
```import org.apache.spark.sql.functions._
object ArrayToColumns {
def main(args: Array[String]): Unit = {
}
}
val dfNew = df.groupBy("ID")
.agg(collect_list("Date").as("DateArr"))