I\'m trying to process data obtained from a csv file using csv module in python. there are about 50 columns & 401125 rows in this. I used the following code chunk to put
As noted by @DSM in the comments, the reason you're getting a memory error is that calling np.size
on a list will copy the data into an array first and then get the size.
If you don't need to work with it as a numpy array, just don't call np.size
. If you do want numpy-like indexing options and so on, you have a few options.
You could use pandas, which is meant for handling big not-necessarily-numerical datasets and has some great helpers and stuff for doing so.
If you don't want to do that, you could define a numpy structure array and populate it line-by-line in the first place rather than making a list and copying into it. Something like:
fields = [('name1', str), ('name2', float), ...]
data = np.zeros((num_rows,), dtype=fields)
csv_file_object = csv.reader(open(r'some_path\Train.csv','rb'))
header = csv_file_object.next()
for i, row in enumerate(csv_file_object):
data[i] = row
You could also define fields
based on header
so you don't have to manually type out all 50 column names, though you'd have to do something about specifying the data types for each.