Jesh Kundem - 4 months ago 33

Python Question

Using python 2.7 through Pycharm

I have a python code - which is very simple.

`a = [2, 3, 4, 5]`

b = [30,32,33,34]

c = ['test1', 'test2', 'test3', 'test4']

a = np.asarray(a) # -- Similarly for b and c

I converted these lists to numpy array.

Then I did the following

`d = np.transpose(np.vstack((a,b,c)))`

Through which I got a 3D numpy array.

when I print d I see a

`u"`

Now how can I access the columns of a,b and do some arithmetic operations on those values. Whenever I am trying to access a value and convert them to int -- it is saying

`invalid literal for int() with base 10`

All I need is read the first or second column -- do some arithmetic operations. First and second column values will be of

`a`

`b`

Answer

Because the last column is text, the other two columns are converted to text also. You can have only one type for one matrix. The solution would be a structured array:

```
a = [2, 3, 4, 5]
b = [30,32,33,34]
c = ['test1', 'test2', 'test3', 'test4']
matrix = np.array(zip(a,b,c), dtype=[('a',int),('b',int),('c','S5')])
```

To access the values, you have to use the column name:

```
matrix['a'][2:4]
```

or

```
matrix[5]['b']
```