Python 如何向数据帧添加插值?

Python 如何向数据帧添加插值?,python,pandas,interpolation,linear-interpolation,Python,Pandas,Interpolation,Linear Interpolation,我有一个名为df的数据帧,名为: timestamp param_1 param_2 0.000 -0.027655 0.0 0.25 -0.034012 0.0 0.50 -0.040369 0.0 0.75 -0.046725 0.0 1.00 -0.050023 0.0 1.25 -0.011015 0.0 1.50 -0.041366 0.0 1.75

我有一个名为df的数据帧,名为:

timestamp   param_1     param_2
0.000       -0.027655   0.0
0.25        -0.034012   0.0
0.50        -0.040369   0.0
0.75        -0.046725   0.0
1.00        -0.050023   0.0
1.25        -0.011015   0.0
1.50        -0.041366   0.0
1.75        -0.056723   0.0
2.00        -0.013081   0.0
现在,我需要添加从以下列表创建的两个新列:

timestamp_new = [0.5, 1.0, 1.5, 2.0]
param_3 = [10.0, 25.0, 15.0, 22.0]
问题在于
timestamp\u new
具有不同的粒度。因此,我需要(线性地)插入
timestamp_new
param_3
,以适应
df
timestamp
的粒度

预期结果(请注意,我随机插入
param_3
值只是为了显示预期结果的格式):

有什么办法吗?

让我们试试
reindex()。插值

ref_df = pd.Series(param_3, index=timestamp_new)
new_vals = (ref_df.reindex(df['timestamp'])
                  .interpolate('index')
                  .bfill()                            # fill the first few nans
                  .ffill()                            # fill the last few nans
           )

df['param_3'] = df['timestamp'].map(new_vals)
输出:

   timestamp   param_1  param_2  param_3
0       0.00 -0.027655      0.0     10.0
1       0.25 -0.034012      0.0     10.0
2       0.50 -0.040369      0.0     10.0
3       0.75 -0.046725      0.0     17.5
4       1.00 -0.050023      0.0     25.0
5       1.25 -0.011015      0.0     20.0
6       1.50 -0.041366      0.0     15.0
7       1.75 -0.056723      0.0     18.5
8       2.00 -0.013081      0.0     22.0
让我们试试
reindex()。插入

ref_df = pd.Series(param_3, index=timestamp_new)
new_vals = (ref_df.reindex(df['timestamp'])
                  .interpolate('index')
                  .bfill()                            # fill the first few nans
                  .ffill()                            # fill the last few nans
           )

df['param_3'] = df['timestamp'].map(new_vals)
输出:

   timestamp   param_1  param_2  param_3
0       0.00 -0.027655      0.0     10.0
1       0.25 -0.034012      0.0     10.0
2       0.50 -0.040369      0.0     10.0
3       0.75 -0.046725      0.0     17.5
4       1.00 -0.050023      0.0     25.0
5       1.25 -0.011015      0.0     20.0
6       1.50 -0.041366      0.0     15.0
7       1.75 -0.056723      0.0     18.5
8       2.00 -0.013081      0.0     22.0

插值可以在
0.5-2.0
范围内进行,但是如何解释
0
0.25
的输出?您只是将
参数3
中的第一个值减少了1吗?@QuangHoang:是的,这两点可以使用启发式方法进行调整。插值可以在
0.5-2.0
范围内进行,但是如何解释
0
0.25
的输出?你只是将参数3中的第一个值减少了1吗?@QuangHoang:是的,这两点可以用启发式方法进行调整。效果很好。非常感谢。它工作得很好。非常感谢。