#python-3.x #pandas #geopandas
Вопрос:
У меня есть проблема, которую я не смог решить, и я надеялся получить здесь некоторые идеи.
У меня есть эти geopandas
таблицы данных:
GEO =
id geometry_zone
0 A001DFD POLYGON ((48.08793 50.93755, 48.08793 49.18650...
1 A001DG POLYGON ((60.96434 49.05222, 59.86796 49.29929...
2 A001DS007 POLYGON ((53.16200 50.20131, 52.84363 48.45026...
3 A001DS01 POLYGON ((59.04953 49.34561, 58.77158 47.52346...
4 A001DS02 POLYGON ((58.12301 49.46915, 57.79873 47.67788...
5 A001DS03 POLYGON ((57.07498 49.66937, 56.79702 47.84722...
6 A001DS04 POLYGON ((56.13302 49.80835, 55.83962 48.00164...
7 A001DS05 POLYGON ((55.16017 49.93189, 54.89766 48.18694...
8 A001DS06 POLYGON ((54.14099 50.05542, 53.86304 48.27959...
9 A001DS08 POLYGON ((52.22678 50.36050, 51.94821 48.52985...
10 A001DS09 POLYGON ((50.93339 48.70894, 51.96811 48.52985...
11 A001DS10 POLYGON ((50.23695 50.67887, 49.91857 48.84823...
12 A001DS11 POLYGON ((50.23695 50.67887, 49.60020 50.75847...
13 A001FS01 POLYGON ((46.47617 48.94772, 46.47617 47.63443...
14 A001FS02 POLYGON ((46.49606 50.04213, 46.47617 48.94772...
centroid
0 POINT (48.75295 49.98494)
1 POINT (60.27696 48.21993)
2 POINT (53.49869 49.22928)
3 POINT (59.29040 48.38586)
4 POINT (58.42620 48.49535)
5 POINT (57.43469 48.68996)
6 POINT (56.46528 48.82210)
7 POINT (55.50608 48.98701)
8 POINT (54.51093 49.10232)
9 POINT (52.52668 49.40021)
10 POINT (51.59314 49.51614)
11 POINT (50.57522 49.68396)
12 POINT (49.74105 49.81923)
13 POINT (47.00679 48.58955)
14 POINT (47.23437 49.55921)
где точки-это geometry_zone
центроиды. Теперь я знаю, как вычислить расстояние между каждой точкой, т. Е. Вычислить матрицу расстояний:
GEO_distances
0 1 2 3 4 5 6
0 0.000000 11.063874 4.299228 10.275246 9.312075 8.274448 7.312941
1 10.983097 0.000000 6.348082 0.616036 1.399226 2.373198 3.374784
2 4.132203 6.259105 0.000000 5.469828 4.507633 3.469029 2.507443
3 9.982697 0.409114 5.348195 0.000000 0.399280 1.373252 2.374671
4 9.112541 1.279148 4.477119 0.487986 0.000000 0.504366 1.503677
5 8.102334 2.289412 3.468492 1.497509 0.538514 0.000000 0.494605
6 7.124643 3.266993 2.490125 2.475753 1.515950 0.474954 0.000000
7 6.151367 4.240258 1.517485 3.448859 2.489192 1.448060 0.487174
8 5.151208 5.240246 0.515855 4.450013 3.488962 2.449214 1.487936
9 3.145284 7.246023 0.481768 6.456493 5.494540 4.455695 3.494278
10 2.205711 8.185458 1.420986 7.396838 6.433798 5.396039 4.434327
11 1.174092 9.217045 2.452510 8.428427 7.465334 6.427628 5.465988
12 0.329081 10.062023 3.297427 9.273461 8.310263 7.272662 6.311059
13 1.235000 12.579303 5.838504 11.812993 10.830385 9.818336 8.852372
14 0.853558 12.484730 5.717153 11.712257 10.730567 9.711458 8.743639
7 8 9 10 11 12 13
0 6.343811 5.312333 3.377798 2.368462 1.343153 0.675055 1.051959
1 4.353762 5.318769 7.388784 8.269175 9.305375 10.325337 12.247130
2 1.538467 0.506829 0.544190 1.416284 2.454398 3.479383 5.430826
3 3.353424 4.318400 6.388838 7.269062 8.304972 9.325272 11.250890
4 2.482659 3.447704 5.519952 6.398068 7.434796 8.456133 10.381205
5 1.473030 2.437971 4.509526 5.388997 6.424600 7.445701 9.379809
6 0.494829 1.459821 3.533033 4.410650 5.446892 6.468964 8.405156
7 0.000000 0.486633 2.560113 3.437762 4.473614 5.495941 7.440721
8 0.518599 0.000000 1.561677 2.436310 3.473427 4.497171 6.443451
9 2.525085 1.493574 0.000000 0.429875 1.467480 2.492644 4.463771
10 3.465481 2.433809 0.499402 0.000000 0.527884 1.554218 3.540493
11 4.497042 3.465439 1.530986 0.521601 0.000000 0.523013 2.556065
12 5.342058 4.310497 2.376017 1.366597 0.341276 0.000000 1.788666
13 7.901132 6.863255 4.941781 3.928417 2.923256 2.273971 0.000000
14 7.782154 6.746808 4.815043 3.790372 2.766326 2.077512 0.492253
14
0 0.703212
1 12.250335
2 5.430658
3 11.253792
4 10.383930
5 9.382000
6 8.406976
7 7.441895
8 6.444094
9 4.461567
10 3.531133
11 2.517604
12 1.686975
13 0.444277
14 0.000000
(Итак, первая строка содержит расстояние до всех точек в centroid
столбце, включая первую точку).
На самом деле я хочу объединить эту матрицу с фреймом данных, и чтобы имена столбцов были id
s от GEO
.
Теперь я знаю, как слиться:
new = GEO.merge(GEO_distances, on=['index'])
который возвращает:
index id geometry_zone
0 0 A001DFD POLYGON ((48.08793 50.93755, 48.08793 49.18650...
1 1 A001DG POLYGON ((60.96434 49.05222, 59.86796 49.29929...
2 2 A001DS007 POLYGON ((53.16200 50.20131, 52.84363 48.45026...
3 3 A001DS01 POLYGON ((59.04953 49.34561, 58.77158 47.52346...
4 4 A001DS02 POLYGON ((58.12301 49.46915, 57.79873 47.67788...
5 5 A001DS03 POLYGON ((57.07498 49.66937, 56.79702 47.84722...
6 6 A001DS04 POLYGON ((56.13302 49.80835, 55.83962 48.00164...
7 7 A001DS05 POLYGON ((55.16017 49.93189, 54.89766 48.18694...
8 8 A001DS06 POLYGON ((54.14099 50.05542, 53.86304 48.27959...
9 9 A001DS08 POLYGON ((52.22678 50.36050, 51.94821 48.52985...
10 10 A001DS09 POLYGON ((50.93339 48.70894, 51.96811 48.52985...
11 11 A001DS10 POLYGON ((50.23695 50.67887, 49.91857 48.84823...
12 12 A001DS11 POLYGON ((50.23695 50.67887, 49.60020 50.75847...
13 13 A001FS01 POLYGON ((46.47617 48.94772, 46.47617 47.63443...
14 14 A001FS02 POLYGON ((46.49606 50.04213, 46.47617 48.94772...
centroid 0 1 2 3
0 POINT (48.75295 49.98494) 0.000000 11.063874 4.299228 10.275246
1 POINT (60.27696 48.21993) 10.983097 0.000000 6.348082 0.616036
2 POINT (53.49869 49.22928) 4.132203 6.259105 0.000000 5.469828
3 POINT (59.29040 48.38586) 9.982697 0.409114 5.348195 0.000000
4 POINT (58.42620 48.49535) 9.112541 1.279148 4.477119 0.487986
5 POINT (57.43469 48.68996) 8.102334 2.289412 3.468492 1.497509
6 POINT (56.46528 48.82210) 7.124643 3.266993 2.490125 2.475753
7 POINT (55.50608 48.98701) 6.151367 4.240258 1.517485 3.448859
8 POINT (54.51093 49.10232) 5.151208 5.240246 0.515855 4.450013
9 POINT (52.52668 49.40021) 3.145284 7.246023 0.481768 6.456493
10 POINT (51.59314 49.51614) 2.205711 8.185458 1.420986 7.396838
11 POINT (50.57522 49.68396) 1.174092 9.217045 2.452510 8.428427
12 POINT (49.74105 49.81923) 0.329081 10.062023 3.297427 9.273461
13 POINT (47.00679 48.58955) 1.235000 12.579303 5.838504 11.812993
14 POINT (47.23437 49.55921) 0.853558 12.484730 5.717153 11.712257
4 5 6 7 8 9 10
0 9.312075 8.274448 7.312941 6.343811 5.312333 3.377798 2.368462
1 1.399226 2.373198 3.374784 4.353762 5.318769 7.388784 8.269175
2 4.507633 3.469029 2.507443 1.538467 0.506829 0.544190 1.416284
3 0.399280 1.373252 2.374671 3.353424 4.318400 6.388838 7.269062
4 0.000000 0.504366 1.503677 2.482659 3.447704 5.519952 6.398068
5 0.538514 0.000000 0.494605 1.473030 2.437971 4.509526 5.388997
6 1.515950 0.474954 0.000000 0.494829 1.459821 3.533033 4.410650
7 2.489192 1.448060 0.487174 0.000000 0.486633 2.560113 3.437762
8 3.488962 2.449214 1.487936 0.518599 0.000000 1.561677 2.436310
9 5.494540 4.455695 3.494278 2.525085 1.493574 0.000000 0.429875
10 6.433798 5.396039 4.434327 3.465481 2.433809 0.499402 0.000000
11 7.465334 6.427628 5.465988 4.497042 3.465439 1.530986 0.521601
12 8.310263 7.272662 6.311059 5.342058 4.310497 2.376017 1.366597
13 10.830385 9.818336 8.852372 7.901132 6.863255 4.941781 3.928417
14 10.730567 9.711458 8.743639 7.782154 6.746808 4.815043 3.790372
11 12 13 14
0 1.343153 0.675055 1.051959 0.703212
1 9.305375 10.325337 12.247130 12.250335
2 2.454398 3.479383 5.430826 5.430658
3 8.304972 9.325272 11.250890 11.253792
4 7.434796 8.456133 10.381205 10.383930
5 6.424600 7.445701 9.379809 9.382000
6 5.446892 6.468964 8.405156 8.406976
7 4.473614 5.495941 7.440721 7.441895
8 3.473427 4.497171 6.443451 6.444094
9 1.467480 2.492644 4.463771 4.461567
10 0.527884 1.554218 3.540493 3.531133
11 0.000000 0.523013 2.556065 2.517604
12 0.341276 0.000000 1.788666 1.686975
13 2.923256 2.273971 0.000000 0.444277
14 2.766326 2.077512 0.492253 0.000000
Но как я могу дать столбцу id
имена простым способом? Ручное переименование 18 000 столбцов — это не мое представление о веселом дне.
Ответ №1:
Я нашел ответ на свой вопрос, но мне все еще интересно, есть ли лучший и более элегантный способ сделать это (например, на лету). Что я сделал, так это:
new_column_name = GEO.id.to_list()
columnlist = GEO_distances.columns.to_list()
cols_remove = ['index','id','geometry_zone','centroid']
old_column_names = [x for x in columnlist if (x not in cols_remove)]
col_rename_dict = {i:j for i,j in zip(old_column_names,new_column_name)}
GEO_distances.rename(columns=col_rename_dict, inplace=True)
что дает:
index id geometry_zone
0 0 A001DFD POLYGON ((48.08793 50.93755, 48.08793 49.18650...
1 1 A001DG POLYGON ((60.96434 49.05222, 59.86796 49.29929...
2 2 A001DS007 POLYGON ((53.16200 50.20131, 52.84363 48.45026...
3 3 A001DS01 POLYGON ((59.04953 49.34561, 58.77158 47.52346...
4 4 A001DS02 POLYGON ((58.12301 49.46915, 57.79873 47.67788...
... ... ... ...
1790 1790 R13C1G POLYGON ((63.72846 54.07087, 61.04155 54.02454...
1791 1791 R13D1A POLYGON ((63.03727 60.43190, 65.27641 57.78312...
1792 1792 R13D1D POLYGON ((68.90781 67.16844, 68.95414 60.51294...
1793 1793 R13D1F POLYGON ((61.42043 67.16403, 75.48019 67.22166...
1794 1794 R13D1G POLYGON ((61.40300 67.15300, 61.43388 63.43148...
centroid A001DFD A001DG A001DS007 A001DS01
0 POINT (48.75295 49.98494) 0.000000 11.063874 4.299228 10.275246
1 POINT (60.27696 48.21993) 10.983097 0.000000 6.348082 0.616036
2 POINT (53.49869 49.22928) 4.132203 6.259105 0.000000 5.469828
3 POINT (59.29040 48.38586) 9.982697 0.409114 5.348195 0.000000
4 POINT (58.42620 48.49535) 9.112541 1.279148 4.477119 0.487986
... ... ... ... ... ...
1790 POINT (62.36165 51.28081) 12.814471 2.630419 8.337061 3.267367
1791 POINT (69.85889 59.16021) 21.991462 13.464194 18.191827 14.124815
1792 POINT (72.22137 63.86261) 26.206982 18.602918 22.776510 19.187716
1793 POINT (68.46954 68.61039) 26.045757 20.948750 23.468535 21.237352
1794 POINT (65.33358 63.93216) 20.589210 15.508162 17.853344 15.717912
A001DS02 A001DS03 A001DS04 A001DS05 A001DS06 A001DS08
0 9.312075 8.274448 7.312941 6.343811 5.312333 3.377798
1 1.399226 2.373198 3.374784 4.353762 5.318769 7.388784
2 4.507633 3.469029 2.507443 1.538467 0.506829 0.544190
3 0.399280 1.373252 2.374671 3.353424 4.318400 6.388838
4 0.000000 0.504366 1.503677 2.482659 3.447704 5.519952
... ... ... ... ... ... ...
1790 3.862726 4.616091 5.526811 6.415345 7.329588 9.280236
1791 14.623171 15.220831 15.921820 16.621702 17.363742 18.956701
1792 19.622822 20.146582 20.757196 21.374153 22.035882 23.454339
1793 21.458123 21.751888 22.104256 22.496392 22.947829 23.939341
1794 15.894837 16.153433 16.481252 16.864660 17.318735 18.347265
Любое другое более эффективное решение приветствуется.