如何打印dfs中的旧值和插值



我有一个名为sodf,如下所示:

gas day    RLM       Date
0   22.03.2020  5501593 2020-03-22
1   23.03.2020  9232167 2020-03-23
2   24.03.2020  8807847 2020-03-24
3   25.03.2020  8561604 2020-03-25
4   26.03.2020  7775652 2020-03-26
5   27.03.2020  56022577 2020-03-27
6   28.03.2020  4556959 2020-03-28
7   29.03.2020  5233497 2020-03-29
8   30.03.2020  8181341 2020-03-30
9   31.03.2020  8063470 2020-03-31

用户可以从RLM列中选择一些值,这些值必须用NaN替换并插值。为此,我正在做:

def spline_interpolate(data: pd.DataFrame,
to_replace: list,
measure: str = 'RLM'):
data_interpolation = data.copy()
data_interpolation[measure] = data_interpolation[measure].replace(
to_replace, np.nan)
data_interpolation[measure] = data_interpolation[measure].interpolate(method='spline',
    order=3)
return data_interpolation

然后,我做:

so_interpolation = spline_interpolate(so, [56022577])

插值后的so_interpolation为:

gas day     RLM         Date
0   22.03.2020  5501593.0   2020-03-22
1   23.03.2020  9232167.0   2020-03-23
2   24.03.2020  8807847.0   2020-03-24
3   25.03.2020  8561604.0   2020-03-25
4   26.03.2020  7775652.0   2020-03-26
5   27.03.2020  5979531.5   2020-03-27
6   28.03.2020  4556959.0   2020-03-28
7   29.03.2020  5233497.0   2020-03-29
8   30.03.2020  8181341.0   2020-03-30
9   31.03.2020  8063470.0   2020-03-31

现在,我想知道是否有一种方法可以自动打印一条声明,说明要替换的值(来自to_replace列表(已被xxxxxxx值替换?

示例:

在上述示例中,值56022577被插值为5979531.5

我想在spline_interpolation()函数中添加一个print语句,该语句自动打印旧值和新插值:

print('The value 56022577 is interpolated as 5979531.5')

p。S.spline_interpolation()函数中的to_replace可以取多个值,因为所有这些值都必须替换为NaNS,然后插值

以下是我如何修改函数:

def spline_interpolate(data,
to_replace,
measure = 'RLM'):
data_interpolation = data.copy()
data_interpolation[measure] = data_interpolation[measure].replace(
to_replace, np.nan)
# where replacements occur
s = data_interpolation[measure].isna()
data_interpolation[measure] = data_interpolation[measure].interpolate(method='spline',
    order=3)
# print as required
for orig,rep in zip(data.loc[s,measure], data_interpolation.loc[s,measure]):
print(f'The value {orig} is interpolated as {rep}')
return data_interpolation

最新更新