I’m using MySQLdb and Python. I have some basic queries such as this:
c=db.cursor()
c.execute("SELECT id, rating from video")
results = c.fetchall()
I need “results” to be a NumPy array, and I’m looking to be economical with my memory consumption. It seems like copying the data row by row would be incredibly inefficient (double the memory would be required). Is there a better way to convert MySQLdb query results into the NumPy array format?
The reason I’m looking to use the NumPy array format is because I want to be able to slice and dice the data easily, and it doesn’t seem like python is very friendly to multi-dimensional arrays in that regard.
e.g. b = a[a[:,2]==1]
Thanks!
The
fetchallmethod actually returns an iterator, and numpy has the fromiter method to initialize an array from an interator. So, depending on what data is in the table you could combine the two easily, or use an adapter generator.