Transpose, then unpack:
>>> x, y, z = data.T
>>> x
array([1, 4, 7])
Answer from behzad.nouri on Stack Overflowpython - Split a numpy column into two columns and keep them in the original array - Stack Overflow
Split a 2d NumPy array into 2 separate 2d arrays based on a column value
numpy - python column split - Stack Overflow
numpy - Python: separate matrix by column values - Stack Overflow
One way of doing it using hstack:
import numpy as np
a = np.array([['burger flipper', 'part time', '12-5.00'],
['spam flipper', 'full time', '98-10.00']])
a = np.hstack((a[:,:2], map(lambda x: x.split('-'), a[:,2])))
print a
Output:
[['burger flipper' 'part time' '12' '5.00']
['spam flipper' 'full time' '98' '10.00']]
A bit of explanation:
The function numpy.hstack allows you to horizontally stack multiple numpy arrays. For example,
np.hstack((a[:,[0,1]], a[:,[2]]))produces the original array
awith three columns. Note the use of brackets ina[:,[2]],[a:,2]will not work as it produces a single dimensional array (len(a[:,2].shape)equals 1).The
mapstatement apply a functionlambda x: x.split('-')to the problematic column (i.e. the 3rd column) of the array. Each call to the lambda function returns a list containing the separated job codes and wage, such as['12', '5.00']. Thus, themapstatement produces a list of list which looks like[['12', '5.00'], ['98', '10.00']]. This can be converted to a numpy array with 2 columns when being fed tohstack.
The code hstack first two columns of the original array with the list of list obtained via map, resulting in an array similar to what you want in the end.
map(lambda x: x.split('-'), a[:,2]) is now giving a list instead of two columns leading to the following error:
ValueError: all the input arrays must have same number of dimensions
Needed to change the previous code to:
import numpy as np
a = np.array([['burger flipper', 'part time', '12-5.00'],
['spam flipper', 'full time', '98-10.00']])
a_newcolumns = np.hstack((map(lambda x: x.split('-'), a[:, 2]))).reshape(a.shape[0], 2)
# need to reshape the list into a two column numpy array
a = np.hstack((a[:, :2], a_newcolumns))
print a
So I have this 2d array that looks something like this
[[1,2,4],[1,4,4],[2,2,4],[2,5,9]]
The first column in this array can only ever be a 1 or a 2, I want to split this 2d array into 2 smaller 2d arrays, with all the arrays where the first column equals 1 are in and another where all the arrays whos column 1 has 2 in it. I am new to numpy and have seen some examples and documents online but i can't seem to get exactly what I want. Here is the current code I am trying to use
```
np.split(trainingData, np.where(trainingData[:, 0]== 1.)[0][1:])
```
I am basically trying to split the trainingData(which is my array) into 2 separate arrays, but when I run this I get more than 2 arrays. If someone could point me on the right path that would be great!
Thank you
I think NumPy is good for this:
>>> import numpy as np
>>> my_list = [[1,2,3],[4,5,6],[7,8,9]]
>>> x = np.array(my_list)
>>> np.transpose(x).tolist()
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
In [85]: [list(x) for x in zip(*[[1,2,3],[4,5,6],[7,8,9]])]
Out[85]: [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
If you want list of tuples you can use:
In [86]: zip(*[[1,2,3],[4,5,6],[7,8,9]])
Out[86]: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
If you're using Numpy, first find the rows where the third column has your desired value, then extract the rows using indexing.
Demo
>>> import numpy
>>> A = numpy.array([[1, 0, 1],
[2, 0, 1],
[3, 0, 0],
[4, 0, 0],
[5, 0, 0]])
>>> A1 = A[A[:, 2] == 1, :] # extract all rows with the third column 1
>>> A0 = A[A[:, 2] == 0, :] # extract all rows with the third column 0
>>> A0
array([[3, 0, 0],
[4, 0, 0],
[5, 0, 0]])
>>> A1
array([[1, 0, 1],
[2, 0, 1]])
>>> a
array([[ 10., 15., 1.],
[ 21., 13., 1.],
[ 9., 14., 0.],
[ 14., 24., 1.],
[ 21., 31., 0.]])
>>> a[np.where(a[:,-1])]
array([[ 10., 15., 1.],
[ 21., 13., 1.],
[ 14., 24., 1.]])
>>> a[np.where(~a[:,-1].astype(bool))]
array([[ 9., 14., 0.],
[ 21., 31., 0.]])