Reading file as array
<p>I have a <em>cvs</em> file which has three columns of numbers up to three digits each:</p>

<pre><code>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
</code></pre>

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

<pre><code>data = np.loadtxt('file.csv')
x = data[:, 0]
y = data[:, 1]
</code></pre>

<p>But I'm getting:</p>

<pre><code>X = np.array(X, dtype)
ValueError: setting an array element with a sequence.
</code></pre>

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

<p>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.</p>

<pre><code>    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 
</code></pre>

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

<pre><code>    df = pd.read_csv(open("name_of_the_csv.csv"))
    for i in xrange(len(df.columns)):
        column_list = df[df.columns[i]].values 
</code></pre>

python arrays numpy