I have a file containing data in the format:
0.0 x1
0.1 x2
0.2 x3
0.0 x4
0.1 x5
0.2 x6
0.3 x7
...
The data consists of multiple datasets, each starting with 0 in the first column (so x1,x2,x3 would be one set and x4,x5,x6,x7 another one). I need to plot each dataset separately so I need to somehow split the data. What would be the easiest way to accomplish this?
I realize I could go through the data line-by-line and split the data every time I encounter a 0 in the first column but this seems very inefficient.
Splitting NumPy Arrays Joining merges multiple arrays into one and Splitting breaks one array into multiple. We use array_split() for splitting arrays, we pass it the array we want to split and the number of splits.
You can also use the function to divide a Numpy array by a scalar value (i.e., divide a matrix by a scalar).
Example-2: This example uses the splice() method to split the array into chunks of array. This method removes the items from the original array. This method can be used repeatedly to split array of any size. | Split array into chunks.
To divide an array into two, we need at least three array variables. We shall take an array with continuous numbers and then shall store the values of it into two different variables based on even and odd values.
I actually liked Benjamin's answer, a slightly shorter solution would be:
B= np.split(A, np.where(A[:, 0]== 0.)[0][1:])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With