不需要迭代dataframe,pandas已经提供了dataframe转sql的方法DataFrame.to_sql(...) .
或者,如果您想手动将数据插入数据库,您可以使用 Pandas 的 to_csv() ,例如:
我有一个这样的 df:
df
A B
first second
bar one 0.826425 -1.126757
two 0.682297 0.875014
baz one -1.714757 -0.436622
two -0.366858 0.341702
foo one -1.068390 -1.074582
two 0.863934 0.043367
qux one -0.510881 0.215230
two 0.760373 0.274389
# set header=False, and index=True to get the MultiIndex from pivot
print df.to_csv(header=False, index=True)
bar,one,0.8264252111679552,-1.1267570930327846
bar,two,0.6822970851678805,0.8750144682657339
baz,one,-1.7147570530422946,-0.43662238320911956
baz,two,-0.3668584476904599,0.341701643567155
foo,one,-1.068390451744478,-1.0745823278191735
foo,two,0.8639343368644695,0.043366628502542914
qux,one,-0.5108806384876237,0.21522973766619563
qux,two,0.7603733646419842,0.2743886250125428
这将为您提供一个很好的逗号分隔格式,可以很容易地在 sql 执行查询中使用,例如:
data = []
for line in df.to_csv(header=False, index=True).split('\n'):
if line:
data.append(tuple(line.split(',')))
data
[('bar', 'one', '0.8264252111679552', '-1.1267570930327846'),
('bar', 'two', '0.6822970851678805', '0.8750144682657339'),
('baz', 'one', '-1.7147570530422946', '-0.43662238320911956'),
('baz', 'two', '-0.3668584476904599', '0.341701643567155'),
('foo', 'one', '-1.068390451744478', '-1.0745823278191735'),
('foo', 'two', '0.8639343368644695', '0.043366628502542914'),
('qux', 'one', '-0.5108806384876237', '0.21522973766619563'),
('qux', 'two', '0.7603733646419842', '0.2743886250125428')]
那么只需要执行一个executemany
:
...
stmt = "INSERT INTO table (first, second, A, B) VALUES (%s, %s, %s, %s)"
cursor.executemany(stmt, data)
...
希望这对您有所帮助。
我是一名优秀的程序员,十分优秀!