如何在 python 中按最接近的几何形状和日期和时间合并 2 个数据帧?



我有2个不同的数据框架。在"weather"数据帧有许多几何图形在同一日期和时间。在"road"数据帧有行字符串,日期和时间,但这里它们不是相同的日期或相同的时间,它们都是不同的。

我想做的是合并2数据帧的日期-时间和几何形状。简而言之,我想在相同的日期和时间用最接近的几何图形组合两个数据帧

我试过这样做

  1. 首先,我试图从"天气"中找到相同的日期和时间。从"道路"开始日期的数据框。dataframe
  2. 然后有多于一个的行和点。我试着找到最近的地方(它在"天气"里)。Dataframe)到行字符串(它在"road"dataframe)
  3. 我想做的最后一件事是删除"天气"中的点(删除那些行)dataframe。

我做了一件事,但我猜是错的。我怎样才能解决这个问题?还有别的办法吗?

这是一个"道路"的例子。dataframe:

{{'roadId': {0: 10500,
1: 10500,
2: 10500,
3: 105400,
4: 105400,
5: 106600,
6: 14100,
7: 22800,
8: 22800,
9: 22800,
10: 29000,
11: 31500,
12: 32100,
13: 36400,
14: 36400,
15: 36600,
16: 40400,
17: 40400,
18: 420253800,
19: 420253800,
20: 420253800,
21: 420253800,
22: 420253800,
23: 420253800,
24: 420253800,
25: 420253800,
26: 420253800,
27: 420253800,
28: 46700,
29: 46700,
30: 48600,
31: 48600,
32: 48600,
33: 48600,
34: 48600,
35: 48600,
36: 48600,
37: 48600,
38: 48600,
39: 48600,
40: 48600,
41: 48600,
42: 53700,
43: 60001200,
44: 60002000,
45: 60002000,
46: 60002000,
47: 60002100,
48: 60002100,
49: 60002100},
'date': {0: '2021.03.08 02:00:00',
1: '2021.03.08 05:45:00',
2: '2021.03.08 04:15:00',
3: '2021.03.07 21:30:00',
4: '2021.03.08 05:45:00',
5: '2021.03.08 05:45:00',
6: '2021.03.07 21:30:00',
7: '2021.03.07 21:30:00',
8: '2021.03.08 01:45:00',
9: '2021.03.08 05:45:00',
10: '2021.03.08 05:45:00',
11: '2021.03.08 05:45:00',
12: '2021.03.08 05:45:00',
13: '2021.03.08 00:00:00',
14: '2021.03.08 04:15:00',
15: '2021.03.08 04:15:00',
16: '2021.03.07 21:30:00',
17: '2021.03.08 04:15:00',
18: '2021.03.07 21:15:00',
19: '2021.03.07 20:45:00',
20: '2021.03.07 21:30:00',
21: '2021.03.07 22:30:00',
22: '2021.03.08 00:30:00',
23: '2021.03.08 01:45:00',
24: '2021.03.08 01:45:00',
25: '2021.03.08 02:00:00',
26: '2021.03.08 02:30:00',
27: '2021.03.08 05:45:00',
28: '2021.03.08 04:15:00',
29: '2021.03.08 05:45:00',
30: '2021.03.07 21:30:00',
31: '2021.03.07 22:30:00',
32: '2021.03.08 01:00:00',
33: '2021.03.08 00:30:00',
34: '2021.03.08 03:45:00',
35: '2021.03.08 02:15:00',
36: '2021.03.08 02:00:00',
37: '2021.03.08 02:30:00',
38: '2021.03.08 05:15:00',
39: '2021.03.08 05:00:00',
40: '2021.03.08 05:45:00',
41: '2021.03.08 04:15:00',
42: '2021.03.08 05:45:00',
43: '2021.03.07 22:30:00',
44: '2021.03.07 23:30:00',
45: '2021.03.08 05:45:00',
46: '2021.03.08 04:15:00',
47: '2021.03.07 21:15:00',
48: '2021.03.08 02:30:00',
49: '2021.03.08 05:45:00'},
'lin': {0: 'LINESTRING (32.85619 39.868728, 32.856321 39.868657, 32.856477 39.868615)',
1: 'LINESTRING (32.85619 39.868728, 32.856321 39.868657, 32.856477 39.868615)',
2: 'LINESTRING (32.85619 39.868728, 32.856321 39.868657, 32.856477 39.868615)',
3: 'LINESTRING (32.882227 39.550864, 32.880966 39.551685)',
4: 'LINESTRING (32.882227 39.550864, 32.880966 39.551685)',
5: 'LINESTRING (32.692448 39.885163, 32.692012 39.885222)',
6: 'LINESTRING (32.86239 39.900284, 32.862381 39.900456)',
7: 'LINESTRING (32.812381 39.878539, 32.81255 39.879431)',
8: 'LINESTRING (32.812381 39.878539, 32.81255 39.879431)',
9: 'LINESTRING (32.812381 39.878539, 32.81255 39.879431)',
10: 'LINESTRING (32.712708 40.404688, 32.711973 40.404542, 32.711355 40.404252, 32.710675 40.403367, 32.71017 40.402972, 32.707958 40.402167, 32.705775 40.40114, 32.704602 40.400377, 32.703107 40.399538, 32.7023 40.399137, 32.701442 40.398798, 32.699727 40.398285, 32.698995 40.397953, 32.69837 40.39751, 32.697003 40.396632, 32.696455 40.396157, 32.69596 40.395658, 32.695558 40.395147, 32.695092 40.394375, 32.694597 40.394143, 32.693903 40.394207, 32.693316 40.394351)',
11: 'LINESTRING (32.651653 39.850165, 32.652376 39.850618)',
12: 'LINESTRING (32.678197 39.867385, 32.678069 39.867217)',
13: 'LINESTRING (32.744751 39.782534, 32.744926 39.786808)',
14: 'LINESTRING (32.744751 39.782534, 32.744926 39.786808)',
15: 'LINESTRING (32.887934 39.952421, 32.887946 39.952489)',
16: 'LINESTRING (32.975037 39.971029, 32.974902 39.971053)',
17: 'LINESTRING (32.975037 39.971029, 32.974902 39.971053)',
18: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
19: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
20: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
21: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
22: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
23: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
24: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
25: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
26: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
27: 'LINESTRING (32.95387116 39.28371696, 32.95342296 39.28059297, 32.953392 39.28009203, 32.95338084 39.27934899, 32.95344888 39.27871998, 32.95354104 39.278205, 32.95368792 39.27774699, 32.95393992 39.27714696, 32.95427184 39.27654603, 32.95456596 39.276108, 32.95491012 39.27567897, 32.95526004 39.27529296, 32.95572588 39.27484098, 32.95624212 39.27442104, 32.95674504 39.274083, 32.95744416 39.27368304, 32.95808784 39.27336903, 32.95879992 39.27308796, 32.95962108 39.27282597, 32.96107512 39.27240198, 32.96199492 39.27213099, 32.96280492 39.27187404, 32.96360808 39.27159297, 32.96435616 39.271302, 32.96566296 39.27076803)',
28: 'LINESTRING (32.871431 39.979258, 32.871111 39.979476, 32.87096 39.979521)',
29: 'LINESTRING (32.871431 39.979258, 32.871111 39.979476, 32.87096 39.979521)',
30: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
31: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
32: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
33: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
34: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
35: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
36: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
37: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
38: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
39: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
40: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
41: 'LINESTRING (32.637394 39.991609, 32.637595 39.991583, 32.637753 39.991543, 32.63852 39.99122)',
42: 'LINESTRING (32.724833 39.996181, 32.724756 39.995943)',
43: 'LINESTRING (32.71277988 39.80887704, 32.71290084 39.80894598, 32.71340196 39.80906001, 32.71361688 39.80909799)',
44: 'LINESTRING (32.76150192 40.014702, 32.76157284 40.01517603)',
45: 'LINESTRING (32.76150192 40.014702, 32.76157284 40.01517603)',
46: 'LINESTRING (32.76150192 40.014702, 32.76157284 40.01517603)',
47: 'LINESTRING (32.707125 40.00806801, 32.70731796 40.00878099, 32.70739392 40.00901598, 32.70750588 40.00922901, 32.70763692 40.00942998, 32.7078 40.00960602, 32.70797604 40.00976901, 32.70823884 40.00998303, 32.70883896 40.010355, 32.70925188 40.010661, 32.70963708 40.01099796, 32.71014 40.01150403, 32.71041792 40.01175396, 32.71069404 40.01193801, 32.71094784 40.01206698, 32.71131396 40.01221602, 32.71158 40.012299, 32.71187592 40.01235903, 32.71401 40.012416)',
48: 'LINESTRING (32.707125 40.00806801, 32.70731796 40.00878099, 32.70739392 40.00901598, 32.70750588 40.00922901, 32.70763692 40.00942998, 32.7078 40.00960602, 32.70797604 40.00976901, 32.70823884 40.00998303, 32.70883896 40.010355, 32.70925188 40.010661, 32.70963708 40.01099796, 32.71014 40.01150403, 32.71041792 40.01175396, 32.71069404 40.01193801, 32.71094784 40.01206698, 32.71131396 40.01221602, 32.71158 40.012299, 32.71187592 40.01235903, 32.71401 40.012416)',
49: 'LINESTRING (32.707125 40.00806801, 32.70731796 40.00878099, 32.70739392 40.00901598, 32.70750588 40.00922901, 32.70763692 40.00942998, 32.7078 40.00960602, 32.70797604 40.00976901, 32.70823884 40.00998303, 32.70883896 40.010355, 32.70925188 40.010661, 32.70963708 40.01099796, 32.71014 40.01150403, 32.71041792 40.01175396, 32.71069404 40.01193801, 32.71094784 40.01206698, 32.71131396 40.01221602, 32.71158 40.012299, 32.71187592 40.01235903, 32.71401 40.012416)'}}
hava=pd.read_csv("C:/Users

这是一个"天气"的例子。dataframe:

{'Lat': {142761: 40.13,
142762: 40.13,
142763: 40.13,
142764: 40.13,
142765: 40.13,
142766: 40.13,
142767: 40.13,
142768: 40.13,
142769: 40.13,
142770: 40.13,
142771: 40.13,
142772: 40.13,
142773: 40.13,
142774: 40.13,
142775: 40.13,
142776: 40.13,
142777: 40.13,
142778: 40.13,
142779: 40.13,
142780: 40.13,
142781: 40.13,
142782: 40.13,
142783: 40.13,
143474: 39.93,
143475: 39.93,
143476: 39.93,
143477: 39.93,
143478: 39.93,
143479: 39.93,
143480: 39.93,
143481: 39.93,
143482: 39.93,
143483: 39.93,
143484: 39.93,
143485: 39.93,
143486: 39.93,
143487: 39.93,
143488: 39.93,
143489: 39.93,
143490: 39.93,
143491: 39.93,
143492: 39.93,
143493: 39.93,
143494: 39.93,
143495: 39.93,
143496: 39.93,
144187: 39.95,
144188: 39.95,
144189: 39.95,
144190: 39.95},
'Lon': {142761: 33.1,
142762: 33.1,
142763: 33.1,
142764: 33.1,
142765: 33.1,
142766: 33.1,
142767: 33.1,
142768: 33.1,
142769: 33.1,
142770: 33.1,
142771: 33.1,
142772: 33.1,
142773: 33.1,
142774: 33.1,
142775: 33.1,
142776: 33.1,
142777: 33.1,
142778: 33.1,
142779: 33.1,
142780: 33.1,
142781: 33.1,
142782: 33.1,
142783: 33.1,
143474: 32.86,
143475: 32.86,
143476: 32.86,
143477: 32.86,
143478: 32.86,
143479: 32.86,
143480: 32.86,
143481: 32.86,
143482: 32.86,
143483: 32.86,
143484: 32.86,
143485: 32.86,
143486: 32.86,
143487: 32.86,
143488: 32.86,
143489: 32.86,
143490: 32.86,
143491: 32.86,
143492: 32.86,
143493: 32.86,
143494: 32.86,
143495: 32.86,
143496: 32.86,
144187: 32.73,
144188: 32.73,
144189: 32.73,
144190: 32.73},
'Tarih': {142761: '2021.03.08',
142762: '2021.03.08',
142763: '2021.03.08',
142764: '2021.03.08',
142765: '2021.03.08',
142766: '2021.03.08',
142767: '2021.03.08',
142768: '2021.03.08',
142769: '2021.03.08',
142770: '2021.03.08',
142771: '2021.03.08',
142772: '2021.03.08',
142773: '2021.03.08',
142774: '2021.03.08',
142775: '2021.03.08',
142776: '2021.03.08',
142777: '2021.03.08',
142778: '2021.03.08',
142779: '2021.03.08',
142780: '2021.03.08',
142781: '2021.03.08',
142782: '2021.03.08',
142783: '2021.03.08',
143474: '2021.03.08',
143475: '2021.03.08',
143476: '2021.03.08',
143477: '2021.03.08',
143478: '2021.03.08',
143479: '2021.03.08',
143480: '2021.03.08',
143481: '2021.03.08',
143482: '2021.03.08',
143483: '2021.03.08',
143484: '2021.03.08',
143485: '2021.03.08',
143486: '2021.03.08',
143487: '2021.03.08',
143488: '2021.03.08',
143489: '2021.03.08',
143490: '2021.03.08',
143491: '2021.03.08',
143492: '2021.03.08',
143493: '2021.03.08',
143494: '2021.03.08',
143495: '2021.03.08',
143496: '2021.03.08',
144187: '2021.03.08',
144188: '2021.03.08',
144189: '2021.03.08',
144190: '2021.03.08'},
'Saat': {142761: '00:00',
142762: '03:00',
142763: '06:00',
142764: '09:00',
142765: '12:00',
142766: '15:00',
142767: '18:00',
142768: '21:00',
142769: '00:00',
142770: '03:00',
142771: '06:00',
142772: '09:00',
142773: '12:00',
142774: '15:00',
142775: '18:00',
142776: '21:00',
142777: '00:00',
142778: '03:00',
142779: '06:00',
142780: '09:00',
142781: '12:00',
142782: '15:00',
142783: '18:00',
143474: '00:00',
143475: '03:00',
143476: '06:00',
143477: '09:00',
143478: '12:00',
143479: '15:00',
143480: '18:00',
143481: '21:00',
143482: '00:00',
143483: '03:00',
143484: '06:00',
143485: '09:00',
143486: '12:00',
143487: '15:00',
143488: '18:00',
143489: '21:00',
143490: '00:00',
143491: '03:00',
143492: '06:00',
143493: '09:00',
143494: '12:00',
143495: '15:00',
143496: '18:00',
144187: '00:00',
144188: '03:00',
144189: '06:00',
144190: '09:00'},
'Weather': {142761: 1,
142762: 1,
142763: 1,
142764: 1,
142765: 1,
142766: 5,
142767: 5,
142768: 1,
142769: 1,
142770: 1,
142771: 5,
142772: 5,
142773: 11,
142774: 5,
142775: 5,
142776: 1,
142777: 7,
142778: 7,
142779: 4,
142780: 4,
142781: 4,
142782: 1,
142783: 4,
143474: 1,
143475: 1,
143476: 1,
143477: 1,
143478: 5,
143479: 5,
143480: 5,
143481: 1,
143482: 1,
143483: 1,
143484: 1,
143485: 5,
143486: 11,
143487: 5,
143488: 1,
143489: 1,
143490: 7,
143491: 7,
143492: 4,
143493: 4,
143494: 4,
143495: 4,
143496: 4,
144187: 1,
144188: 1,
144189: 1,
144190: 1},
'geometry': {142761: <shapely.geometry.point.Point at 0x1ca1d671948>,
142762: <shapely.geometry.point.Point at 0x1ca1d671b08>,
142763: <shapely.geometry.point.Point at 0x1ca1d671ac8>,
142764: <shapely.geometry.point.Point at 0x1ca1d671a88>,
142765: <shapely.geometry.point.Point at 0x1ca1d6716c8>,
142766: <shapely.geometry.point.Point at 0x1ca1d671808>,
142767: <shapely.geometry.point.Point at 0x1ca1d671788>,
142768: <shapely.geometry.point.Point at 0x1ca1d671908>,
142769: <shapely.geometry.point.Point at 0x1ca1d671248>,
142770: <shapely.geometry.point.Point at 0x1ca1d671388>,
142771: <shapely.geometry.point.Point at 0x1ca1d671c08>,
142772: <shapely.geometry.point.Point at 0x1ca1d671d48>,
142773: <shapely.geometry.point.Point at 0x1ca1d671c88>,
142774: <shapely.geometry.point.Point at 0x1ca1d671688>,
142775: <shapely.geometry.point.Point at 0x1ca1d6717c8>,
142776: <shapely.geometry.point.Point at 0x1ca1d671e88>,
142777: <shapely.geometry.point.Point at 0x1ca1d671988>,
142778: <shapely.geometry.point.Point at 0x1ca1d671e48>,
142779: <shapely.geometry.point.Point at 0x1ca1d671dc8>,
142780: <shapely.geometry.point.Point at 0x1ca1d671d08>,
142781: <shapely.geometry.point.Point at 0x1ca1d671d88>,
142782: <shapely.geometry.point.Point at 0x1ca1d671f48>,
142783: <shapely.geometry.point.Point at 0x1ca1d671b48>,
143474: <shapely.geometry.point.Point at 0x1ca1d6943c8>,
143475: <shapely.geometry.point.Point at 0x1ca1d694548>,
143476: <shapely.geometry.point.Point at 0x1ca1d694388>,
143477: <shapely.geometry.point.Point at 0x1ca1d6942c8>,
143478: <shapely.geometry.point.Point at 0x1ca1d694688>,
143479: <shapely.geometry.point.Point at 0x1ca1d6945c8>,
143480: <shapely.geometry.point.Point at 0x1ca1d694648>,
143481: <shapely.geometry.point.Point at 0x1ca1d694108>,
143482: <shapely.geometry.point.Point at 0x1ca1d6947c8>,
143483: <shapely.geometry.point.Point at 0x1ca1d694488>,
143484: <shapely.geometry.point.Point at 0x1ca1d694788>,
143485: <shapely.geometry.point.Point at 0x1ca1d694708>,
143486: <shapely.geometry.point.Point at 0x1ca1d6948c8>,
143487: <shapely.geometry.point.Point at 0x1ca1d6946c8>,
143488: <shapely.geometry.point.Point at 0x1ca1d694888>,
143489: <shapely.geometry.point.Point at 0x1ca1d694848>,
143490: <shapely.geometry.point.Point at 0x1ca1d6949c8>,
143491: <shapely.geometry.point.Point at 0x1ca1d694808>,
143492: <shapely.geometry.point.Point at 0x1ca1d694748>,
143493: <shapely.geometry.point.Point at 0x1ca1d694b08>,
143494: <shapely.geometry.point.Point at 0x1ca1d694a48>,
143495: <shapely.geometry.point.Point at 0x1ca1d694ac8>,
143496: <shapely.geometry.point.Point at 0x1ca1d694588>,
144187: <shapely.geometry.point.Point at 0x1ca1d6b7408>,
144188: <shapely.geometry.point.Point at 0x1ca1d6b73c8>,
144189: <shapely.geometry.point.Point at 0x1ca1d6b7548>,
144190: <shapely.geometry.point.Point at 0x1ca1d6b7388>}}
下面是我的代码:
liste={}
lis={}
for i in range(road.date.count()):            
for n in range(weather.Tarih.count()):
if (weather.index == (142600+n)).any()==True:
if road.date[i][0:10]==weather.Tarih[142600+n]:
if str(pd.Timestamp(road.date[i][11:16]).round('180min').to_pydatetime())[11:16]==weather.Saat[142600+n]:
point = weather.geometry[142600+n]
liste[142600+n]=point
if len(liste)!=0:
print(liste)
for a, p1 in liste.items():
line = shapely.wkt.loads(road.lin[i])
p2 = nearest_points(line, p1)[0]
distance = math.sqrt(((p1.x-p2.x)**2)+((p1.y-p2.y)**2))
lis[a]=distance
b=min(lis.values())
b=bul(b,lis)
lis.pop(b)
for k in liste.keys():
weather.drop([k], axis=0, inplace=True)
liste.clear()
lis.clear()

此代码用于按日期和时间合并数据帧

road['date'] = pd.to_datetime(road['date'])
road = road.sort_values(by = 'date')
weather['date'] = weather['Tarih'] + ' ' + weather['Saat']
weather['date'] = pd.to_datetime(weather['date'])
weather = weather.sort_values(by = 'date')
newdf = pd.merge_asof(left = road, right = weather, on = 'date', tolerance = pd.Timedelta('180min')) #in weather dataframe there is a difference of 3 hours between the hours like 0-3-6-9

但是当我运行第一段代码然后运行这段代码在某个点之后它变成了nan value

你应该使用direction="nearest",否则你只会期待

newdf = pd.merge_asof(left = road, right = weather, on = 'date', tolerance = pd.Timedelta('180min'), direction="nearest")

同样,有了这个容差,你无论如何都会有一个nan,但你可以增加它,以去除所有的nans。

最新更新