使用:Python的2.7.1在Windows填充缺失值
你好,我担心这个问题,有一个非常简单的答案,但我似乎无法找到一个适当和有效的解决方案(我有有限的python经验)。我正在编写一个应用程序,从第三方API(wundergorund)下载历史气象数据。事情是,有时在给定的时间内没有价值(例如,我们在凌晨5点有20度,上午6点没有值,上午7点有21度)。我需要在任何给定的小时内确切地获得一个温度值,所以我想我可以适合我所拥有的数据并评估我失踪的点(使用SciPy的polyfit)。这很酷,但是,我在处理程序时遇到问题以检测列表是否缺少小时,如果是,插入缺少的小时并计算温度值。我希望是有道理的..
我在处理的时间和温度列表尝试如下:
from scipy import polyfit
# Evaluate simple cuadratic function
def tempcal (array,x):
return array[0]*x**2 + array[1]*x + array[2]
# Sample data, note it has missing hours.
# My final hrs list should look like range(25), with matching temperatures at every point
hrs = [1,2,3,6,9,11,13,14,15,18,19,20]
temps = [14.0,14.5,14.5,15.4,17.8,21.3,23.5,24.5,25.5,23.4,21.3,19.8]
# Fit coefficients
coefs = polyfit(hrs,temps,2)
# Cycle control
i = 0
done = False
while not done:
# It has missing hour, insert it and calculate a temperature
if hrs[i] != i:
hrs.insert(i,i)
temps.insert(i,tempcal(coefs,i))
# We are done, leave now
if i == 24:
done = True
i += 1
我明白为什么这不工作,该方案最终将试图访问索引超出hrs列表的范围。我也知道修改循环内的列表长度必须小心。当然,我要么不够小心,要么完全忽视更简单的解决方案。
在我的搜索帮助中,我碰到了熊猫(图书馆),但我觉得我可以在没有它的情况下解决这个问题(我宁愿这么做)。
任何输入,非常感谢。非常感谢。
您应该使用'dictionary'而不是2个列表:'weather_dict = {1:14.0,2:14.5,3:14.5,4:None等}'。您可以使用所有的任意值初始化“dict”,然后填写您拥有的数据。 – IanAuld
谢谢你,会给它一个机会! – cilop