pythonpandassqlite

Why do I get NotImplementedError trying to create a dataframe from a database table?


Using Pandas I can't read from an SQLite table:

import pandas as pd
import sqlite3 as sq

with sq.connect("master.db") as con:
    table = "personal"
    df = pd.read_sql_table(table, con)

Database file and code are in the same directory. Python raises "NotImplementedError".


Solution

  • For the entire table you can use pd.read_sql_query() and a SELECT * FROM table query:

    import pandas as pd
    import sqlite3 as sq
    
    with sq.connect("master.db") as con:
       yourParams = ["personal"]
       df = pd.read_sql_query("SELECT * FROM ?", con, params = yourParams)
    

    Note the parametrized query to prevent SQL-injection. You can also use pd.read_sql_table() (its parameters are the table name and database file, not the connection):

    import pandas as pd
    import sqlite3 as sq
    
    df = pd.read_sql_table('personal','master.db')