Read in the first column of a CSV in python
<p>I have a CSV (mylist.csv) with 2 columns that look similar to this:</p>

<blockquote>
<pre><code>jfj840398jgg     item-2f
hd883hb2kjsd     item-9k
jie9hgtrbu43     item-12
fjoi439jgnso     item-3i
</code></pre>
</blockquote>

<p>I need to read the first column into a variable so I just get:</p>

<blockquote>
<pre><code>jfj840398jgg
hd883hb2kjsd
jie9hgtrbu43
fjoi439jgnso
</code></pre>
</blockquote>

<p>I tried the following, but it is only giving me the first letter of each column:</p>

<pre><code>import csv
list2 = []
with open("mylist.csv") as f:
    for row in f:
        list2.append(row[0])
</code></pre>

<p>so the results of the above code are giving me list2 as:</p>

<blockquote>
  <p>['j', 'h', 'j', 'f']</p>
</blockquote>

<p>You can also use <a href="http://pandas.pydata.org/" rel="nofollow"><code>pandas</code></a> here:</p>

<pre><code>import pandas as pd
df = pd.read_csv(mylist.csv)
</code></pre>

<p>Then, getting the first column is as easy as:</p>

<pre><code>matrix2 = df[df.columns[0]].as_matrix
list2 = matrix2.tolist()
</code></pre>

<p>This will return only the first column in <code>list</code>. You might want to consider leaving the data in <code>numpy</code>, if you're conducting further data operation on the result you get.</p>

python csv