简体   繁体   中英

Fastest and most efficient way to put a numpy array to Mysql db

So I have a two numpy array. One of the arrays contain the object_ids for my database and all of these are ints and another contains the values I want to put to my database which are floats. Also my database is a mysql db.

what is the most efficient way to put all of this to my mysql db cause Im looking to optimize my code.

im also using MySQLdb and cursors to connect to my database.

You can use pandas as a clean interface between the two worlds. As an example :

import numpy as np
import pandas as pd

index = np.arange(10)
values = np.random.rand(10,2)
names= ['a','b']

df= pd.DataFrame(values,index,names)

In [217]: df
Out[217]: 
          a         b
0  0.259787  0.313253
1  0.449761  0.363243
2  0.916497  0.204994
3  0.168862  0.718966
4  0.248206  0.106945
5  0.709540  0.902679
6  0.413442  0.933243
7  0.568530  0.427908
8  0.926834  0.967445
9  0.175371  0.952798

You can then use df.to_sql which will tranfer you data in a SQL database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM