user357269 - 3 months ago 38

Python Question

I have

`row.csv`

`1, 2, 3, 4, 5`

and

`column.csv`

`1,`

2,

3,

4,

5

`numpy.genfromtxt("one-of-the-above.csv", delimiter=",")`

`np.array([1,2,3,4,5])`

but I would like to get distinct 2D arrays instead.

Answer

That is a "feature" of `genfromtxt`

. Before it returns the data that it read from the file, it uses `numpy.squeeze`

to eliminate trivial dimensions.

If you use `loadtxt`

instead, you can use the argument `ndmin=2`

to ensure that the result is always a two-dimensional array.

For example:

```
In [15]: !cat one_row.csv
1, 2, 3, 4, 5
In [16]: !cat one_col.csv
1
2
3
4
5
In [17]: np.loadtxt('one_row.csv', delimiter=',', ndmin=2)
Out[17]: array([[ 1., 2., 3., 4., 5.]])
In [18]: np.loadtxt('one_col.csv', delimiter=',', ndmin=2)
Out[18]:
array([[ 1.],
[ 2.],
[ 3.],
[ 4.],
[ 5.]])
```

If you would like to see this option added to `genfromtxt`

, add a comment or a "thumbs up" to the issue that I created for it here: https://github.com/numpy/numpy/issues/4811