Interpolate above and below a range of values in a column - Pandas

Viewed 248

I was looking for the way to extend the range values inside a Pandas column by interpolation, but I still don't know how to set the 'limits' of the interpolation, I mean, it's something like:

  [Distance]      [Radiation]
     12               120
     13               130
     14               140
     15               150
     16               160
     17               170

So, what I'm trying to get is the full range of column [Radiation] according to the complete secuence of column [Distance] by interpolation.

  [Distance]      [Radiation]
      1               10
      2               20
      .                .
      .                .
      12              120
      13              130
      14              140
      15              150
      16              160
      .                .
      .                .
      20              200

I was looking in the documentation of pandas and scipy methods but I think I couldn't find it yet. Thanks for your insights.

1 Answers

One idea is use DataFrame.reindex for add all not existing values of distance and then use DataFrame.interpolate with barycentric method:

df = (df.set_index('Distance')
        .reindex(range(1, 21))
        .interpolate(method='barycentric', limit_direction='both')
        .reset_index())
print (df)
    Distance  Radiation
0          1       10.0
1          2       20.0
2          3       30.0
3          4       40.0
4          5       50.0
5          6       60.0
6          7       70.0
7          8       80.0
8          9       90.0
9         10      100.0
10        11      110.0
11        12      120.0
12        13      130.0
13        14      140.0
14        15      150.0
15        16      160.0
16        17      170.0
17        18      180.0
18        19      190.0
19        20      200.0
Related