0

I am trying to import some data from the database (Postgre SQL) to work with them in Python. I tried with the code below, which seems quite similar to the ones I've found on the internet.

import psycopg2
import sqlalchemy as db
import pandas as pd

engine = db.create_engine('database specifications')
connection = engine.connect()
metadata = db.MetaData()
data = db.Table(tabela, metadata, schema=shema, autoload=True, autoload_with=engine)
query = db.select([data])
ResultProxy = connection.execute(query)
ResultSet = ResultProxy.fetchall()
df = pd.DataFrame(ResultSet)

However, it returns data without column names. What did I forget?

Vesnič
  • 365
  • 5
  • 17

1 Answers1

0

It turned out the only thing needed is adding

columns = data.columns.keys()
df.columns = columns

There is a great debate about that in this thread.

Vesnič
  • 365
  • 5
  • 17