By Michael Nocito, data analyst · Published August 8, 2026

By the end of this page you will run a SQL query from Python and get back a pandas DataFrame with real column names, ready to chart or export. It takes three lines, needs nothing installed beyond pandas, and works the same way against Postgres and MySQL once you swap the connection.

Here is what to actually do today. Use pd.read_sql(query, connection) rather than a cursor and fetchall(). Both work. Only one of them hands you column names, and the other hands you a list of anonymous tuples you then have to label by counting positions.

The short version: sqlite3.connect() for a database file, pd.read_sql() for the query, and pass values as parameters instead of building the SQL string yourself.

The difference between the two ways of fetching is the one idea worth the page, so it gets the picture.