Reputation: 11
I'm trying to import data from sqlite database and load it into a pandas DataFrame
. Sounds easy right?
import pandas as pd
import sqlite3 as sql
pd.set_option('precision',7)
db = sql.connect('D:\db\crypto_db.db')
cursor = db.cursor()
cursor.execute('''select * from price''')
data_sql = cursor.fetchall()
data_pd = pd.DataFrame(data_sql)
print(data_pd)
The import works fine. This is a list of tuples. As the dataframe
gets loaded with data_sql
I get the following error:
AttributeError: 'list' object has no attribute 'name'
In my code I do not 'attribute' a list at all. I suppose it is in the underlying code of Pandas, but I cannot figure it out.
Please help and make my day :)
First row looks like this:
(1, '2018-01-22 20:57:37.952722', 4.779e-05, 8.567e-05, 0.14788838, 5.5e-07, 8.455e-05, 2.437e-05, 4.662e-05, 0.015833, 0.00016368, 2.958e-05, 4.16e-06, 0.00070031, 6.174e-05, 0.07073803, 0.00877186, 4.75e-06, 6.2e-07, 5.252e-05, 0.00267409, 0.09210015, 0.00045663, 0.00421175, 3.51e-06, 1.532e-05, 0.00031989, 0.00417836, 0.0189958, 5.415e-05, 8.64e-06, 2.335e-05, 5.801e-05, 0.00193177, 0.01658499, 6.69e-05, 0.00025703, 0.00069553, 0.0003491, 3.828e-05, 3.603e-05, 0.00141013, 0.00490978, 0.0002373, 4.37e-06, 2.012e-05, 0.00046392, 0.00086303, 0.00769998, 2.241e-05, 0.00060928, 3.45e-06, 0.00038887, 0.000126, 4.374e-05, 0.00109395, 5.831e-05, 0.00034156, 0.000115, 0.00039507, 0.00938508, 0.00523241, 8.691e-05, 0.02860075, 9.441e-05, 0.00011479, 5.874e-05, 0.040542, 0.00015748, 1.59910467, 0.00066517, 0.02921625, 0.04512196, 0.20561388, 0.00058784, 0.02107231, 0.01526, 0.083361, 0.00414773, 0.44203455, 0.00170822, 1552.20855, 10512.99999903, 745.17442254, 28.31224896, 968.53360056, 176.02677305, 0.3806, 81.99986775, 0.46, 298.601292, 1.202, 432.5895432, 1.972e-05, 0.00159783, 0.55736974, 2.44854735, 0.57825471, 0.00233897, 0.00125385, 1.41373884)
I also tried:
import pandas as pd
import sqlite3 as sql
conn = sql.connect('D:\db\crypto_db.db')
data_pd = pd.read_sql('select * from price',conn)
but this gives the next error:
AttributeError: 'list' object has no attribute 'is_unique'
Upvotes: 1
Views: 694
Reputation: 9018
pandas
can actually do extract + convert to dataframe at the same time.
You can do:
pd.read_sql(con=cursor, sql="select * from price")
Reference: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_sql.html
Upvotes: 1