pyspark-fuzzy日期匹配或连接

new9mtju  于 2021-05-27  发布在  Spark
关注(0)|答案(0)|浏览(205)

我需要在中创建一个二进制变量 main_df 以双方最接近的日期为条件 ind_df .

main=spark.createDataFrame([("J1", "2016-10-01 00:42:00", 100), ("J1", "2016-10-01 11:45:30", 200), 
                          ("J1", "2016-10-05 16:20:00", 100), ("J9", "2016-10-06 08:35:00", 300), 
                          ("J9", "2016-10-07 01:20:00", 200)], ["id", "date", "value"]).show()

ind_df=spark.createDataFrame([("J1", "2016-10-01 00:15:30"), ("J1", "2016-10-01 11:45:40"), 
                          ("J9", "2016-10-07 01:20:50")], ["id", "date"]).show()

+---+-------------------+-----+
| id|               date|value|
+---+-------------------+-----+
| J1|2016-10-01 00:42:00|  100|
| J1|2016-10-01 11:45:30|  200|
| J1|2016-10-05 16:20:00|  100|
| J9|2016-10-06 08:35:00|  300|
| J9|2016-10-07 01:20:00|  200|
+---+-------------------+-----+

+---+-------------------+
| id|               date|
+---+-------------------+
| J1|2016-10-01 00:15:30|
| J1|2016-10-01 11:45:40|
| J9|2016-10-07 01:20:50|
+---+-------------------+

所需Dataframe:

+---+-------------------+-----+-----+
| id|               date|value| ind |
+---+-------------------+-----+-----+
| J1|2016-10-01 00:42:00|  100|  1  |
| J1|2016-10-01 11:45:30|  200|  1  |
| J1|2016-10-05 16:20:00|  100|  0  |
| J9|2016-10-06 08:35:00|  300|  0  |
| J9|2016-10-07 01:20:00|  200|  1  |
+---+-------------------+-----+-----+

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题