回答編集履歴

2

Update

2022/05/27 03:28

投稿

melian
melian

スコア19840

test CHANGED
@@ -1,8 +1,7 @@
1
1
  ```python
2
2
  dfx = df1.merge(df2, how='cross', suffixes=('_1', '_2'))
3
3
  dfx['distance'] = dfx.apply(lambda x: cal_distance(*x), axis=1)
4
- df3 = dfx.groupby(['lon_1', 'lat_1'], as_index=False)\
4
+ df3 = dfx.iloc[dfx.groupby(['lon_1', 'lat_1'])['distance'].idxmin()].reset_index(drop=True)
5
- .apply(lambda x: x.loc[x['distance'].idxmin()])
6
5
 
7
6
  print(df3)
8
7
  #

1

Update

2022/05/27 03:18

投稿

melian
melian

スコア19840

test CHANGED
@@ -1,11 +1,15 @@
1
1
  ```python
2
2
  dfx = df1.merge(df2, how='cross', suffixes=('_1', '_2'))
3
3
  dfx['distance'] = dfx.apply(lambda x: cal_distance(*x), axis=1)
4
+ df3 = dfx.groupby(['lon_1', 'lat_1'], as_index=False)\
4
- min_pair = dfx.iloc[[dfx['distance'].idxmin()]]
5
+ .apply(lambda x: x.loc[x['distance'].idxmin()])
5
6
 
6
- print(min_pair)
7
+ print(df3)
8
+ #
9
+ lon_1 lat_1 lon_2 lat_2 distance
10
+ 0 130.8 35.1 131.5 35.1 63.823879
11
+ 1 135.8 36.1 134.5 35.1 161.825625
12
+ 2 140.8 37.1 134.5 35.1 609.174484
13
+ 3 145.8 38.1 134.5 35.1 1064.473702
7
14
 
8
- #
9
- lon_1 lat_1 lon_2 lat_2 distance
10
- 0 130.8 35.1 131.5 35.1 63.823879
11
15
  ```