1

我有一个看起来像这样的 .csv

value,interpolated,what_it_should_be
34,,34
,,25
25,,25
3,,3
,,5

该文件作为 pandas 数据框被读入 python。我想对缺失的数据进行插值,但插值必须在 5-25(含)之间

   value  interpolated  what_it_should_be
0   34.0          34.0               34.0
1    NaN          29.5               25.0
2   25.0          25.0               25.0
3    3.0           3.0                3.0
4    NaN           3.0                5.0

这就是我到目前为止所拥有的。我需要帮助的是限制插值的范围。

import pandas as pd

file = 'test.csv'
df = pd.read_csv(file)

df['interpolated'] = df['value'].interpolate(method='linear')

print(df)
4

1 回答 1

1

clip然后我们可以fillna返回

df.value.fillna(df.interpolated.clip(lower=5,upper=25))
0    34.0
1    25.0
2    25.0
3     3.0
4     5.0
Name: value, dtype: float64
于 2019-12-01T23:28:03.417 回答