Py学习  »  Python

在python中,如何在数据帧索引列表上循环300个数据点?

astralled • 5 年前 • 1429 次点击  

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')      
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53036
 
1429 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Joe B
Reply   •   1 楼
Joe B    6 年前

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')
blueteeth
Reply   •   2 楼
blueteeth    6 年前

看来你可以在 flux_maxij . 所以你大概也可以访问切片?

您可以使用

flux_maxij[0:300]
# OR
start = 0
flux_maxij[start:start+300]

以及 init_list 似乎包含[0,300,600,…]

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')

这样行吗?不知道是什么很难理解 是。