我有一个看起来像这样的 .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)