[英]How do I iterate a loop over 300 data points over list of dataframe indices in python?
我有超過12,000秒的數據點,並且創建了一個循環,在300秒的間隔內並不是很好,並且需要在循環中使用索引。 我該怎么做,以便我可以在需要時提取有關第一個300s間隔和以后的第二個間隔的信息; 我該如何修復現有代碼呢?
init_list = [] # initial values for chunks
median_list = [] # list of median values for 300 s intervals
holding_list = [] # hold values up till what you tell it to
pos_count = 0 # 0 = position 1 for python
for i in range(len(flux_maxij)):
holding_list.append(flux_maxij[i]) # append means add on to
if pos_count == 0: # '==' means IF it is this value
init_list.append(i)
if pos_count == 299: # 299 = 300 which is the 'end' of the range
holding_list.sort() #make it pretty
median_list.append(holding_list[149]) # half of 300 is 150, 149 for python
holding_list = []
pos_count = -1 # -1+1 = o, position 1 when it loops back
pos_count += 1
x = np.array([init_list]) # makes arrays for x and y to graph it
y = np.array([median_list])
plt.plot(x,y, 's')
看來您可以訪問flux_maxij
索引。 因此,大概您也可以訪問切片?
您可以通過以下方式訪問前300個項目
flux_maxij[0:300]
# OR
start = 0
flux_maxij[start:start+300]
並且init_list
似乎包含[ init_list
,...]
init_list = list(range(0, len(flux_maxij), 300)) # range from 0 to the total length, jumping 300 each time
median_list = []
for i in init_list:
holding = sorted(flux_maxij[i:i+300]) # get the next bit of the list and sort it
median_list.append(holding[149]) # append the median
x = np.array([init_list])
y = np.array([median_list])
plt.plot(x, y, 's')
那樣有用嗎? 不知道什么是flux_maxij
很難理解。
為什么不將您的列表分為300個相等大小的段,其中包括:
interval = 300
divided_list = [flux_maxij[i:i+interval] for i in range(0, len(flux_maxij), interval)]
您可以看到range如何接受三個參數: range(init_value, end_value, step_size)
然后為每個間隔打印(或存儲)所需的值:
x = np.array(range(0, len(flux_maxij), interval))
y = np.array([np.median(j) for j in divided_list])
plt.plot(x, y, 's')
這樣一來便變成:
plt.plot(np.array(range(0, len(flux_maxij), interval)),
np.array([np.median(j) for j in [flux_maxij[i:i+interval] for i in range(0, len(flux_maxij), interval)]]),
's')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.