Python Forum
sequential sampling from an array to a matrix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sequential sampling from an array to a matrix
#1
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!
Reply
#2
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sequential Decision Making erdemath 2 1,098 Feb-10-2023, 06:30 AM
Last Post: erdemath
  Creating look up table/matrix from 3d data array chai0404 3 2,824 Apr-09-2020, 04:53 AM
Last Post: buran
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 2,927 Apr-09-2019, 04:54 PM
Last Post: PhysChem
  Array to construct matrix Felipe 1 3,870 Jan-16-2017, 06:11 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020