所以我试图平均每600行。我得到一个超出范围的Python错误索引,数字不正确。帮助吗
import numpy as np
data = np.loadtxt('Y:/Canopy/PSUone.txt', delimiter = ',', skiprows = 0)
shape = np.shape(data)
data_you_want = data[:,3]
daily_averages = list()
for i,d in enumerate(data_you_want):
if (i % 600) == 0:
avg_for_day = np.mean(data_you_want[i - 600:i])
daily_averages.append(avg_for_day)
with open('Y:/Canopy/newPSUone.txt', 'w') as f:
f.write('Wind U Direction\n')
f.write('------------------\n')
for i, d in enumerate(data_you_want):
s = '{0:4.3f}\n'. format(daily_averages[i])
f.write(s)
f.close()
答案 0 :(得分:0)
我认为你遇到的问题是:
for i,d in enumerate(data_you_want):
if (i % 600) == 0:
avg_for_day = np.mean(data_you_want[i - 600:i])
daily_averages.append(avg_for_day)
......紧随其后:
for i, d in enumerate(data_you_want):
s = '{0:4.3f}\n'. format(daily_averages[i])
f.write(s)
因为您只是从data_you_want
到平均值daily_averages
的第600行,所以len(data_you_want) // 600
中将包含daily_averages
个元素。如果您迭代data_you_want
并尝试daily_average[i]
,那么您将达到i
大于daily_averages
长度的点,您将获得错误。