Search code examples
pythonpandasinterpolationlatitude-longitude

Pandas - fill missing lat long cordinates by interpolation


With dataframe like below

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

Need to fill in the NaN values for Lat and Long fields such that each row with NaN values for Lat / Long takes value such that:

  1. they fall on a straight line between the next (say x2,y2) and
  2. the previous non NaN lat/long (say x1,y1) points and are spaced equally between them.

In the above case, since there are three rows with NaN for Lat/Long, they need to take 3 equally spaced points between the non-NaN rows

Is there a way to achieve this with pandas or should it be done outside?

Update:

Tried df.interpolate() as suggested in comments - that works!!


Solution

  • Tried df.interpolate() as suggested in comments - that works!!

    (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