Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

How to initialise a polars dataframe with column names from database cursor description?

$
0
0

I'm connecting to Postgres Database using psycopg2 connector and using cursor property, i'm fetching the records along with their column names. Here is the code snippet of the same:

rds_conn = psycopg2.connect(        host=config.RDS_HOST_NAME,        database=config.RDS_DB_NAME,        user=config.RDS_DB_USER,        password=config.RDS_DB_PASSWORD,        port=config.RDS_PORT)    cur = rds_conn.cursor()    cur.execute(sql_query)    names = [x[0] for x in cur.description]    rows = cur.fetchall()    cur.close()

I'm initialising a Pandas dataframe with column names from the cursor.description property:

 df = pd.DataFrame(rows, columns=names)

Instead of Pandas dataframe, if i would like to initialise a Polars dataframe with column names from the cursor.description property, how would i do it? I don't want to convert Pandas to Polars dataframe.

I tried using polars.Dataframe.with_Columns property but it didn't work.

Can someone please help me on this?


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>