Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a single column of a CSV and store in an array

Tags:

python

pandas

csv

What is the best way to read from a csv, but only one specific column, like title?

ID | date|  title |
-------------------
  1|  2013|   abc |
  2|  2012|   cde |

The column should then be stored in an array like this:

data = ["abc", "cde"]

This is what I have so far, with pandas:

data = pd.read_csv("data.csv", index_col=2)

I've looked into this thread. I still get an IndexError: list index out of range.

EDIT:

It's not a table, it's comma seperated like this:

ID,date,title
1,2013,abc
2,2012,cde
like image 886
dh762 Avatar asked Jan 11 '14 18:01

dh762


2 Answers

One option is just to read in the entire csv, then select a column:

data = pd.read_csv("data.csv")

data['title']  # as a Series
data['title'].values  # as a numpy array

As @dawg suggests, you can use the usecols argument, if you also use the squeeze argument to avoid some hackery flattening the values array...

In [11]: titles = pd.read_csv("data.csv", sep=',', usecols=['title'], squeeze=True)

In [12]: titles  # Series
Out[12]: 
0    abc
1    cde
Name: title, dtype: object

In [13]: titles.values  # numpy array
Out[13]: array(['abc', 'cde'], dtype=object)
like image 194
Andy Hayden Avatar answered Sep 20 '22 13:09

Andy Hayden


You can do something like this:

>>> import pandas as pd
>>> from StringIO import StringIO
>>> txt='''\
... ID,date,title
... 1,2013,abc
... 2,2012,cde'''
>>> data=pd.read_csv(StringIO(txt), usecols=['title']).T.values.tolist()[0]
>>> data
['abc', 'cde']

Or, assuming that you have some blanks:

txt='''\
ID,date,title
1,2013,abc
2,2012,cde
3,2014, 
4,2015,fgh'''
table=pd.read_csv(StringIO(txt), usecols=['title'])
print table
  title
0   abc
1   cde
2      
3   fgh
data=pd.read_csv(StringIO(txt), usecols=['title']).T.values.tolist()[0]
print data
['abc', 'cde', ' ', 'fgh']

Or if you have variable number of data fields:

txt='''\
ID,date,title
1,2013,
2,2012,cde
3
4,2015,fgh'''

print pd.read_csv(StringIO(txt), usecols=['title'])
  title
0   NaN
1   cde
2   NaN
3   fgh

print pd.read_csv(StringIO(txt), usecols=['title']).T.values.tolist()[0]
[nan, 'cde', nan, 'fgh']
like image 40
dawg Avatar answered Sep 20 '22 13:09

dawg