sequential sampling from an array to a matrix - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: sequential sampling from an array to a matrix (/thread-25368.html) |
sequential sampling from an array to a matrix - nsansen - Mar-28-2020 Dear all i have an Array A A=[0 1 2 3 4 5 6 7 8 9 10 ... 200] from this array i would like to make a matrix B (pandas dataframe) where each row of B will equal 3 sequential data points from A i.e. B[0]=0 1 2 B[1]=1 2 3 B[2]=2 3 4 B[3]=3 4 5 ... B[197]=197 188 199 B[198]=198 199 200 I failed to do it with .loc and for loops due to indexing problems. Can someone help me? Thanks in advance! RE: sequential sampling from an array to a matrix - deanhystad - Mar-29-2020 Slicing in a list comprehension? # Make a list to slice inputs = [i for i in range(21)] # Slice it up matrix = [inputs[i:i+3] for i in range(0, len(inputs)-2)] print(matrix)Or a generator def list_slicer(the_list, slices): for i in range(0, len(inputs)-slices+1): yield the_list[i:i+slices] for row in list_slicer(inputs, 3): print(row) |