2017-02-06 5 views
1

これはいくつかのスレッドでは簡単なトピックですが、解決策を見つけることができませんでした。Pandas Dataframe to Sqlite - バインディング問題

私が持っているものは、配列を読み込んでそれをDataframeに変換するtxtファイルです。 その後、私はそれが日付/数字であるように単一の列を変換します。私は探しているものを構築するためのより効率的な方法があると確信していますが、それは私のために働いた。

病気はそのように見えるのデータフレームで終わる:

PSVLD Market Date PSVLD Ticker PSVLD Tenor Date PSVLD Percent of Spot \ 
1   2017-02-03  .MSCIEA    1M     50.0 
2   2017-02-03  .MSCIEA    1M     60.0 
3   2017-02-03  .MSCIEA    1M     70.0 
4   2017-02-03  .MSCIEA    1M     75.0 
5   2017-02-03  .MSCIEA    1M     80.0 
6   2017-02-03  .MSCIEA    1M     85.0 
7   2017-02-03  .MSCIEA    1M     90.0 
8   2017-02-03  .MSCIEA    1M     92.5 
9   2017-02-03  .MSCIEA    1M     95.0 
10  2017-02-03  .MSCIEA    1M     97.5 

    PSVLD Vol 
1 34.96749 
2 34.36383 
3 32.58459 
4 30.53958 
5 28.30699 
6 24.74774 
7 20.07822 
8 17.38867 
9 14.58027 
10 11.84767 

そして今、私はこのデータをデータベースに挿入します。私は新しいテーブルを作成し、このデータフレームを自分のファイルに実行しようとしましたが、動作しません。

con = sq3.connect('my_db.db') 

query = 'CREATE TABLE ImpliedVola (Date date, Ticker varchar(50), Tenor varchar(10),Strike real, IV real)' 
con.execute(query) 
con.commit() 
con.executemany('INSERT INTO ImpliedVola VALUES (?, ?, ?, ?, ?)', df) 

しかし、不幸にも約17のバインディングがあります。あなたは私が間違っていることを知っていますか?前もって感謝します!

ProgrammingError: Incorrect number of bindings supplied. The current statement uses 5, and there are 17 supplied. 
+0

それでも、誰かがここで私が間違って何をしたか知っていますか?なぜなら、私はそれが正しいとすれば、私はupdaterまたはinsertでクエリを置き換えることができるからです。 –

答えて

0

ネイティブのPandas DataFrame.to_sql()メソッドを使用します。

デモ:

In [41]: import sqlite3 

In [42]: con = sqlite3.connect('d:/temp/df.sqlite') 

In [43]: df 
Out[43]: 
    PSVLD Market Date PSVLD Ticker PSVLD Tenor Date PSVLD Percent of Spot PSVLD Vol 
0  2017-02-03  .MSCIEA    1M     50.0 34.96749 
1  2017-02-03  .MSCIEA    1M     60.0 34.36383 
2  2017-02-03  .MSCIEA    1M     70.0 32.58459 
3  2017-02-03  .MSCIEA    1M     75.0 30.53958 
4  2017-02-03  .MSCIEA    1M     80.0 28.30699 
5  2017-02-03  .MSCIEA    1M     85.0 24.74774 
6  2017-02-03  .MSCIEA    1M     90.0 20.07822 
7  2017-02-03  .MSCIEA    1M     92.5 17.38867 
8  2017-02-03  .MSCIEA    1M     95.0 14.58027 
9  2017-02-03  .MSCIEA    1M     97.5 11.84767 

In [44]: df.rename(columns=lambda x: x.replace(' ', '_')).to_sql('ImpliedVola', con, if_exists='replace') 

結果:

D:\temp>sqlite3 df.sqlite 
SQLite version 3.10.1 2016-01-13 21:41:56 
Enter ".help" for usage hints. 
sqlite> .mode column 
sqlite> .header on 
sqlite> select * from ImpliedVola; 
index  PSVLD_Market_Date PSVLD_Ticker PSVLD_Tenor_Date PSVLD_Percent_of_Spot PSVLD_Vol 
---------- ------------------- ------------ ---------------- --------------------- ---------- 
0   2017-02-03 00:00:00 .MSCIEA  1M    50.0     34.96749 
1   2017-02-03 00:00:00 .MSCIEA  1M    60.0     34.36383 
2   2017-02-03 00:00:00 .MSCIEA  1M    70.0     32.58459 
3   2017-02-03 00:00:00 .MSCIEA  1M    75.0     30.53958 
4   2017-02-03 00:00:00 .MSCIEA  1M    80.0     28.30699 
5   2017-02-03 00:00:00 .MSCIEA  1M    85.0     24.74774 
6   2017-02-03 00:00:00 .MSCIEA  1M    90.0     20.07822 
7   2017-02-03 00:00:00 .MSCIEA  1M    92.5     17.38867 
8   2017-02-03 00:00:00 .MSCIEA  1M    95.0     14.58027 
9   2017-02-03 00:00:00 .MSCIEA  1M    97.5     11.84767 
+0

ありがとうございます。これは動作しますが、to_sql()はテーブル内のすべての以前のデータを置き換えます。私は毎日データを追加したいので、それを回避する方法がわかりません。 –

+0

@StefanMüller、単純に 'if_exists = 'append''を使用してください – MaxU

+0

別の質問 いくつかの主キーを宣言して既存の値を更新できますか? sqlのUPDATEと同様ですか? –