rm24
rm24

Reputation: 27

Splitting a 2D numpy array into blocks based on row

I have a block of data from a csv file i have converted into a numpy array of x,y and z values. the data is a series of lines, i have organised the data so that the lines are sorted by x and then y values so get the correct order i want.

The problem i have is that this data contains a series of lines, i want to split the array into blocks a section for each line that i can work on. i have some code that can find the points i require to split the array:

for i in range(0,array_len):
    if data_sort[i][0] == data_sort[i+1][0]:
        pass
    else:
        print("split")

where data_sort contains my array and the code checks if the values in the column change. Where the consecutive column values change i need to split the array. the print is just there to test i had the correct number of splits which i did.

does anyone have a good way of doing this without needing to know the final array size required?

Edit if the input data was:

array([[-0.18798 ,  0.028104, -0.14745 ],
   [-0.18798 ,  0.028214, -0.14732 ],
   [-0.11279 ,  0.028188, -0.21054 ],
   [-0.11279 ,  0.028214, -0.21052 ],
   [-0.18798 ,  0.028214,  0.14732 ],
   [-0.18798 ,  0.028104,  0.14745 ],
   [-0.037596,  0.028   , -0.23602 ],
   [-0.037596,  0.028214, -0.23585 ],
   [ 0.      ,  0.028   , -0.23899 ],
   [ 0.      ,  0.028214, -0.23883 ]])

I would like to split the array into the following way:

    [array([[-0.18798 ,  0.028104, -0.14745 ],
        [-0.18798 ,  0.028214, -0.14732 ],
        [-0.11279 ,  0.028188, -0.21054 ],
        [-0.11279 ,  0.028214, -0.21052 ],
        [-0.18798 ,  0.028214,  0.14732 ]]),
    array([[-0.18798 ,  0.028104,  0.14745 ],
        [-0.037596,  0.028   , -0.23602 ],
        [-0.037596,  0.028214, -0.23585 ],
        [ 0.      ,  0.028   , -0.23899 ],
        [ 0.      ,  0.028214, -0.23883 ]])]

This was done using np.split(test,2), but this simply splits the array into even blocks

Upvotes: 0

Views: 850

Answers (1)

abhilb
abhilb

Reputation: 5757

You can use np.split as shown below.

Say you want to split the array into 4 groups. group1 - index 0 to 1 group2 - index 2 to 3 group3 - index 4 to 8 group4 - index 9 to 10

then you can do

np.split(arr, [1,3,8])

which will give

[array([[-0.18798 ,  0.028104, -0.14745 ]]), array([[-0.18798 ,  0.028214, -0.14732 ],
       [-0.11279 ,  0.028188, -0.21054 ]]), array([[-0.11279 ,  0.028214, -0.21052 ],
       [-0.18798 ,  0.028214,  0.14732 ],
       [-0.18798 ,  0.028104,  0.14745 ],
       [-0.037596,  0.028   , -0.23602 ],
       [-0.037596,  0.028214, -0.23585 ]]), array([[ 0.      ,  0.028   , -0.23899 ],
       [ 0.      ,  0.028214, -0.23883 ]])]

Upvotes: 1

Related Questions