Pandas DataFrame to SqLite

I am learning how to write dataFrame pandas for SQLite db.

I went in one example code:

import pandas as pd import pandas.io.sql as pd_sql import sqlite3 as sql con = sql.connect("/home/msalese/Documents/ipyNotebooks/tmp.db") df =pd.DataFrame({'TestData':[1,2,3,4,5,6,7,8,9]}) pd_sql.write_frame(df, "tbldata2", con) 

But the above exception throws an exception:

 --------------------------------------------------------------------------- InterfaceError Traceback (most recent call last) <ipython-input-31-c844f7e3f2e6> in <module>() ----> 1 pd_sql.write_frame(df, "tbldata2", con) /opt/epdFree7.3.2/lib/python2.7/site-packages/pandas-0.10.1-py2.7-linux-x86_64.egg/pandas/io/sql.pyc in write_frame(frame, name, con, flavor, if_exists, **kwargs) 208 if func is None: 209 raise NotImplementedError --> 210 func(frame, name, safe_names, cur) 211 cur.close() 212 con.commit() /opt/epdFree7.3.2/lib/python2.7/site-packages/pandas-0.10.1-py2.7-linux-x86_64.egg/pandas/io/sql.pyc in _write_sqlite(frame, table, names, cur) 219 table, col_names, wildcards) 220 data = [tuple(x) for x in frame.values] --> 221 cur.executemany(insert_query, data) 222 223 def _write_mysql(frame, table, names, cur): InterfaceError: Error binding parameter 0 - probably unsupported type. 

I think the problem is with code 220. If I try:

 [tuple(x) for x in df.values] 

result:

 [(1,), (2,), (3,), (4,), (5,), (6,), (7,), (8,), (9,)] 

can be commas giving noise for sqlite db.

I'm not sure if anyone can give me a hint, please?

+8
python pandas sqlite
source share
1 answer

Check out the answer from @unutbu in the comments.

The problem is resolved if you specify a float type. For example,

 df = pd.DataFrame({'TestData': [1, 2, 3, 4, 5, 6, 7, 8, 9]}, dtype='float') 
+2
source share

All Articles