简体   繁体   中英

Pandas interpolate between two rows

I have a pandas dataframe with N rows and M columns. Each column of this dataframe describes an almost continuous value. Sometime I need to substitute a row value with the value interpolated from the rows above and below the selected row.

example: here is my dataframe

                energy         dist
0.0           0.177927     0.031584
1.0           0.221856     0.040009
2.0           0.270373     0.049613
3.0           0.322914     0.060317
4.0           0.374736     0.071943
5.0           0.428795     0.084314
6.0           0.480093     0.097233
7.0           0.527760     0.110482
8.0           0.575735     0.123875
9.0           0.619085     0.137201
10.0          0.657782     0.150029
11.0          6.955060    3.623098
12.0          0.730219     0.173924
13.0          0.763311     0.184680
14.0          0.792759     0.194432
15.0          0.820090     0.203171
16.0          0.846813     0.211012
17.0          0.871730     0.218134
18.0          0.896308     0.224660
19.0          0.919440     0.230689
20.0          0.942667     0.236376

I need to change row 11 with the interpolation between row 10 and row 12 (for example a mean value between two rows)

                energy         dist
0.0           0.177927     0.031584
1.0           0.221856     0.040009
2.0           0.270373     0.049613
3.0           0.322914     0.060317
4.0           0.374736     0.071943
5.0           0.428795     0.084314
6.0           0.480093     0.097233
7.0           0.527760     0.110482
8.0           0.575735     0.123875
9.0           0.619085     0.137201
10.0          0.657782     0.150029
11.0          0.694000    0.161976
12.0          0.730219     0.173924
13.0          0.763311     0.184680
14.0          0.792759     0.194432
15.0          0.820090     0.203171
16.0          0.846813     0.211012
17.0          0.871730     0.218134
18.0          0.896308     0.224660
19.0          0.919440     0.230689
20.0          0.942667     0.236376

How is the most efficient way in pandas to do this ?

Thanks

Set misisng values and then use Series.interpolate :

df.loc[11, 'dist'] = np.nan
df['dist'] = df['dist'].interpolate()
print (df)
        energy      dist
0.0   0.177927  0.031584
1.0   0.221856  0.040009
2.0   0.270373  0.049613
3.0   0.322914  0.060317
4.0   0.374736  0.071943
5.0   0.428795  0.084314
6.0   0.480093  0.097233
7.0   0.527760  0.110482
8.0   0.575735  0.123875
9.0   0.619085  0.137201
10.0  0.657782  0.150029
11.0  6.955060  0.161976
12.0  0.730219  0.173924
13.0  0.763311  0.184680
14.0  0.792759  0.194432
15.0  0.820090  0.203171
16.0  0.846813  0.211012
17.0  0.871730  0.218134
18.0  0.896308  0.224660
19.0  0.919440  0.230689
20.0  0.942667  0.236376

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM