What's the most efficient way to convert a MySQL result set to a NumPy array?

前端 未结 3 1756
暖寄归人
暖寄归人 2020-12-01 13:00

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()
         


        
3条回答
  •  执笔经年
    2020-12-01 13:29

    NumPy's fromiter method seems best here (as in Keith's answer, which preceded this one).

    Using fromiter to recast a result set, returned by a call to a MySQLdb cursor method, to a NumPy array is simple, but there are a couple of details perhaps worth mentioning.

    import numpy as NP
    import MySQLdb as SQL
    
    cxn = SQL.connect('localhost', 'some_user', 'their_password', 'db_name')
    c = cxn.cursor()
    c.execute('SELECT id, ratings from video')
    
    # fetchall() returns a nested tuple (one tuple for each table row)
    results = cursor.fetchall()
    
    # 'num_rows' needed to reshape the 1D NumPy array returend by 'fromiter' 
    # in other words, to restore original dimensions of the results set
    num_rows = int(c.rowcount)
    
    # recast this nested tuple to a python list and flatten it so it's a proper iterable:
    x = map(list, list(results))              # change the type
    x = sum(x, [])                            # flatten
    
    # D is a 1D NumPy array
    D = NP.fromiter(iterable=x, dtype=float, count=-1)  
    
    # 'restore' the original dimensions of the result set:
    D = D.reshape(num_rows, -1)
    

    Note that fromiter returns a 1D NumPY array,

    (This makes sense, of course, because you can use fromiter to return just a portion of a single MySQL Table row, by passing a parameter for count).

    Still, you'll have to restore the 2D shape, hence the predicate call to the cursor method rowcount. and the subsequent call to reshape in the final line.

    Finally, the default argument for the parameter count is '-1', which just retrieves the entire iterable

提交回复
热议问题