python - 如何从 dfs 打印旧值和插值?
问题描述
我有一个如下df
命名:so
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
,这些值必须替换为 aNaN
并进行插值。为此,我正在做:
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
。
我想在自动打印旧值和新插值print
的函数中添加一条语句:spline_interpolation()
print('The value 56022577 is interpolated as 5979531.5')
PS:to_replace
在spline_interpolation()
函数中可以取多个值,因为所有这些值都必须替换为NaN
s 并随后进行插值
解决方案
以下是我将如何修改函数:
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