Python 如何从dfs打印旧值和插值?
我有一个名为Python 如何从dfs打印旧值和插值?,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有一个名为so的df,如下所示: 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
so
的df
,如下所示:
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\u插值
为:
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
现在,我想知道是否有一种方法可以自动打印一条语句,说明要替换的值(从到\u replace
列表)已被xxxxxxx
值替换
示例:
在上述示例中,值56022577
被插值为5979531.5
我想在spline\u interpolation()
函数中添加一个print
语句,自动打印旧值和新插值:
print('The value 56022577 is interpolated as 5979531.5')
p.S.:
替换样条插值()函数中的可以取多个值,因为所有这些值都必须替换为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