【问题标题】:Pandas - fill missing lat long cordinates by interpolationPandas - 通过插值填充缺失的经纬度坐标
【发布时间】:2020-01-28 15:14:11
【问题描述】:

使用如下数据框

Time    Lat    Long
19:24:52.135    35.61067    139.630228
19:24:52.183    NaN NaN
19:24:52.281    NaN NaN
19:24:52.378    NaN NaN
19:24:52.466    35.610692   139.630428

需要为 LatLong 字段填写 NaN 值,以便 Lat / Long 具有 NaN 值的每一行的值如下:

  1. 它们落在下一个(比如 x2,y2)和
  2. 之间的直线上
  3. 之前的非 NaN 纬度/经度(比如 x1,y1)点,它们之间的间距相等。

在上述情况下,由于 Lat/Long 有 3 行 NaN,因此它们需要在非 NaN 行之间取 3 个等距点

有没有办法用 pandas 实现这一点,还是应该在外面完成?

更新:

按照 cmets 中的建议尝试了 df.interpolate() - 有效!

【问题讨论】:

  • 在这种情况下做一个简单的df.interpolate() 工作..?
  • df.ffill() ?阅读更多here
  • 我相信@Chris 的想法应该可行,但请记住,您可能想要处理 180 和 90 坐标周围的环绕 :)

标签: python pandas interpolation latitude-longitude


【解决方案1】:

按照 cmets 中的建议尝试了 df.interpolate() - 有效!!

(Pdb) df["Long"].interpolate(method='linear')
0    139.630228
1    139.630278
2    139.630328
3    139.630378
4    139.630428
Name: Long, dtype: float64
(Pdb) df["Long"].interpolate()
0    139.630228
1    139.630278
2    139.630328
3    139.630378
4    139.630428
Name: Long, dtype: float64

【讨论】:

    【解决方案2】:

    你可以试试这个(这是 Lat 的解决方案,Long 也可以这样做):

    df = pd.DataFrame({'Lat':[35.61069, np.nan, np.nan, np.nan, 35.610692], 'Long': [139.630428, np.nan, np.nan, np.nan, 139.630428]})
    
    df
             Lat        Long
    0  35.610690  139.630428
    1        NaN         NaN
    2        NaN         NaN
    3        NaN         NaN
    4  35.610692  139.630428
    

    让我们用最后一个非南纬值创建一个新列

    df['Lat_shift'] = df['Lat'].shift()
    df['Lat_shift'] = df['Lat_shift'].fillna(method='ffill')
    
    df
             Lat        Long  Lat_shift
    0  35.610690  139.630428        NaN
    1        NaN         NaN   35.61069
    2        NaN         NaN   35.61069
    3        NaN         NaN   35.61069
    4  35.610692  139.630428   35.61069
    

    现在我们可以计算我们想要的任何指标:

    df['Lat_new'] = df['Lat_shift'] + (df['Lat'] - df['Lat_shift'])/2
    
             Lat        Long  Lat_shift    Lat_new
    0  35.610690  139.630428        NaN        NaN
    1        NaN         NaN   35.61069        NaN
    2        NaN         NaN   35.61069        NaN
    3        NaN         NaN   35.61069        NaN
    4  35.610692  139.630428   35.61069  35.610691
    

    并使用它来填充 nan 值:

    df.loc[pd.isnull(df['Lat']), 'Lat'] = df['Lat_new'].fillna(method='bfill')
    
    df.drop(columns=['Lat_shift', 'Lat_new'])
    
             Lat        Long
    0  35.610690  139.630428
    1  35.610691         NaN
    2  35.610691         NaN
    3  35.610691         NaN
    4  35.610692  139.630428
    

    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      相关资源
      最近更新 更多