0

I have a cvs file which has three columns of numbers up to three digits each:

1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
6 0 0
7 0 0
8 0 0
9 0 0
10 0 0
11 0 0

I want to read the columns separately and be able to use them as arrays with:

data = np.loadtxt('file.csv')
x = data[:, 0]
y = data[:, 1]

But I'm getting:

X = np.array(X, dtype)
ValueError: setting an array element with a sequence.

If instead I use the line x,y = np.loadtxt('Beamprofile.txt', usecols=(0,1), unpack=True) the error disappears but x and y don't seem to be read correctly in further operations.

2 Answers2

0

Simplest solution to this could be to use pandas module in python, it gives the freedom to use column values as numpy array, which you can convert to list easily.

    import pandas as pd

    df = pd.read_csv(open("name_of_the_csv.csv"))
    # For one column, lets say the 1st one
    column_1_list = df[df.columns[0]].values 

If you want to access all the columns you can use for loop and do it like shown below:

    df = pd.read_csv(open("name_of_the_csv.csv"))
    for i in xrange(len(df.columns)):
        column_list = df[df.columns[i]].values 
0

With your sample:

In [1]: data=np.loadtxt('stack39174768.txt')
In [2]: data
Out[2]: 
array([[  1.,   0.,   0.],
       [  2.,   0.,   0.],
       [  3.,   0.,   0.],
       [  4.,   0.,   0.],
       [  5.,   0.,   0.],
       [  6.,   0.,   0.],
       [  7.,   0.,   0.],
       [  8.,   0.,   0.],
       [  9.,   0.,   0.],
       [ 10.,   0.,   0.],
       [ 11.,   0.,   0.]])
In [3]: x=data[:,0]
In [4]: y=data[:,1]
In [5]: x
Out[5]: array([  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.])
In [6]: y
Out[6]: array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

Where's the problem? Is there's something about the file that we're not seeing in the clip?

In [8]: x,y = np.loadtxt('stack39174768.txt', usecols=(0,1), unpack=True)

produces the same x and y.

I asked about shape and dtype

In [11]: data.shape
Out[11]: (11, 3)
In [12]: data.dtype
Out[12]: dtype('float64')

I like np.genfromtxt a little better, but in this case it produces the same data.

hpaulj
  • 221,503
  • 14
  • 230
  • 353