Question

I have this array:

data = [[  1.00000000e-01   1.00000000e-03]
 [  2.00000000e-01   2.00000000e-03]
 [  3.00000000e-01   3.00000000e-03]
 [  4.00000000e-01   4.00000000e-03]
 [  5.00000000e-01   5.00000000e-03]
 [  6.00000000e-01   6.00000000e-03]
 [  7.00000000e-01   7.00000000e-03]
 [  8.00000000e-01   8.00000000e-03]
 [  9.00000000e-01   9.00000000e-03]
 [  1.00000000e+00   1.00000000e-02]
 [  1.10000000e+00   1.10000000e-02]
 [  1.20000000e+00   1.20000000e-02]
 [  1.30000000e+00   1.30000000e-02]
 [  1.40000000e+00   5.23359560e-02]
 [  1.50000000e+00   1.04528463e-01]
 [  1.60000000e+00   1.56434465e-01]
 [  1.70000000e+00   2.07911691e-01]
 [  1.80000000e+00   2.58819045e-01]
 [  1.90000000e+00   3.09016994e-01]
 [  2.00000000e+00   3.58367950e-01]
 [  2.10000000e+00   4.06736643e-01]
 [  2.20000000e+00   4.53990500e-01]
 [  2.30000000e+00   5.00000000e-01]
 [  2.40000000e+00   5.44639035e-01]
 [  2.50000000e+00   5.87785252e-01]
 [  2.60000000e+00   6.29320391e-01]
 [  2.70000000e+00   6.69130606e-01]
 [  2.80000000e+00   7.07106781e-01]
 [  2.90000000e+00   7.43144825e-01]
 [  3.00000000e+00   7.77145961e-01]
 [  3.10000000e+00   8.09016994e-01]
 [  3.20000000e+00   8.38670568e-01]
 [  3.30000000e+00   8.66025404e-01]
 [  3.40000000e+00   8.91006524e-01]
 [  3.50000000e+00   9.13545458e-01]
 [  3.60000000e+00   9.33580426e-01]
 [  3.70000000e+00   9.51056516e-01]
 [  3.80000000e+00   9.65925826e-01]
 [  3.90000000e+00   9.78147601e-01]
 [  4.00000000e+00   9.87688341e-01]
 [  4.10000000e+00   9.94521895e-01]
 [  4.20000000e+00   9.98629535e-01]
 [  4.30000000e+00   1.00000000e+00]
 [  4.40000000e+00   1.00100000e+00]
 [  4.50000000e+00   1.00200000e+00]
 [  4.60000000e+00   1.00300000e+00]
 [  4.70000000e+00   1.00400000e+00]
 [  4.80000000e+00   1.00500000e+00]
 [  4.90000000e+00   1.00600000e+00]
 [  5.00000000e+00   1.00700000e+00]]

and I need to create a second array from it that is made of the first value of each row:

rows = [[1.00000000e-01], [2.00000000e-01], ...., [5.00000000e+00]]

Is there a one line way to do this? Thanks

Was it helpful?

Solution

Set up a sample array:

In [17]: a = np.arange(10).reshape(5,2)

In [18]: a
Out[18]:
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])

Then depending on the shape of the array resulting from the first value in each row:

In [19]: a[:,0]
Out[19]: array([0, 2, 4, 6, 8])

or

In [20]: a[:,0][:,None] # or a[:,0,None] or a[:,0,np.newaxis]
Out[20]:
array([[0],
       [2],
       [4],
       [6],
       [8]])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top