Extract and convert data to numpy

Suppose I have the following numpy vector

[[1, 3., 'John Doe', 'male', 'doc', '25'],
  ...,
 [9, 6., 'Jane Doe', 'female', 'p', '28']]

I need to extract data from my task.

Being new to numpy and python in general, I would do it like this:

data = np.array(
[[1, 3., 'John Doe', 'male', 'doc', 25],
 [9, 6., 'Jane Doe', 'female', 'p', 28]]
)

data_tr = np.zeros((data.shape[0], 3))
for i in range(0, data.shape[0]):
    data_tr[i][0] = data[i, 1]
    data_tr[i][1] = 0 if data[i, 3] == 'male' else 1
    data_tr[i][2] = data[i, 5]

And as a result, I have the following:

[[  3.,   0.,  25.],
 [  6.,   1.,  28.]]

I would like to know if there is a more efficient or clean way to accomplish this.
Can someone help me with this?

+6
source share
1 answer

One approach with column-indexing-

data_tr = np.zeros((data.shape[0], 3))
data_tr[:,[0,2]] = data[:, [1,5]]
data_tr[:,1] = data[:,3]=='male'

, : data_tr[:,[0,2]] = data[:, [1,5]] . . , , , :

data_tr[:,0] = data[:, 1]
data_tr[:,2] = data[:, 5]
+5

All Articles