0

I am working on a Flask project where I need to query certain entries from a database and perform some computations on them. I use Flask-SQLAlchemy to load/update/query the database, and Pandas definitely looks like the best choice to perform the computations I need.

But I need to efficiently convert the output to Pandas first. The only ways to do it that I found through Google search were through SQLAlchemy, which, as far as I understand, has a different syntax from Flask-SQLAlchemy. Any clues how I could proceed without switching to SQLAlchemy?

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Max C
  • 3
  • 4
  • 1
    Does this answer your question? [SQLAlchemy ORM conversion to pandas DataFrame](https://stackoverflow.com/questions/29525808/sqlalchemy-orm-conversion-to-pandas-dataframe) – Yaakov Bressler Apr 05 '23 at 03:03

1 Answers1

0

Are you using sqlite? if you are then you can use...

import pandas as pd
import os, sqlite3

dirname = os.path.dirname(os.path.realpath(__file__))
path = os.path.join(dirname, 'data.sqlite')

cnx = sqlite3.connect(path)
sqlstatement = f"SELECT * FROM table ;"
df = pd.read_sql_query(sqlstatement, cnx, dtype={'id':int})

You will need to make sure your sqlstatement is right. You might used WHERE to filter the data etc.

Shane S
  • 1,747
  • 14
  • 31