Trying to convert psycopg2 -> Python data from list tuples to dict

Question:

I have a postgreSQL table called stockdata with financial statement information for about 250 companies with the following setup:

Company  q4_2022_revenue q3_2022_revenue
CPI Card 126436000       124577000  
Zuora    103041000       101072000 
…

I used the psycopg2 package in Python to import this data into a ‘main.py’ file with the following code:

import psycopg2

conn=psycopg2.connect(‘dbname=postgres user=postgres password=‘…’)
cur=conn.cursor()
cur.execute(‘SELECT company, (q4_2022_revenue-q3_2022_revenue)/CAST(q3_2022_revenue AS float) AS revenue_growth FROM stockdata ORDER BY revenue_growth DESC LIMIT 25;
records=cur.fetchall()
print(records)

Which gave me the following results:

[(‘Alico’, 9.269641125121241),(‘Arrowhead Pharmaceuticals’, 1.7705869324473975),…],

where records[0] is equal to (‘Alico’, 9.269641125121241) and the type for records as a whole was given as list.

I’m trying to figure out how I can assign a variable separately to both the company name and change in revenue so I can modify the revenue change from, for example, 9.2696 to 926.96% (the company is in a seasonal industry – agriculture – and is recovering from a hurricane so the data makes sense). The revenue change would be multiplied by 100 and given a % sign on the end and rounded to 2 decimal digits.

I tried adding the line:

list((x,y) for x,y in records)

with the intention of assigning x as key values for company name and y as pair values for revenue change, but I received a ‘name ‘x’ is not defined’ error when calling records[x].

How can I convert my psycopg2 list of tuples to a mutable dict and assign the results to a key-value pair so I can modify the formatting of the values?

EDIT: Typo in 926.96%.

Asked By: jon bon journo

||

Answers:

In psycopg2.extras there is the RealDictCursor which can output query results as dicts.

Change your cursor to use it as below:

conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
Answered By: Devasta

To assign the results of your query to a dictionary with company name as the key and revenue growth as the value, you can loop through the tuples in the list and add them to a dictionary like this:

for record in records:
    company = record[0]
    revenue_growth = record[1]
    result_dict[company] = revenue_growth

This will create a dictionary where each key is a company name and the corresponding value is the revenue growth. You can then modify the formatting of the revenue growth values using string formatting like this:

 for company, revenue_growth in result_dict.items():
        formatted_revenue_growth = '{:.2f}%'.format(revenue_growth * 100)
        result_dict[company] = formatted_revenue_growth

This will loop through the dictionary and replace the original revenue growth values with formatted strings that display the percentage change with two decimal places.

Note that dictionaries are mutable by nature, so you can modify the values directly without converting to a mutable data type. However, if you prefer to work with a mutable data type, you can convert the list of tuples to a dictionary using dictionary comprehension like this:

result_dict = {record[0]: record[1] for record in records}

This will create a dictionary where each key is a company name and the corresponding value is the revenue growth. You can then modify the values as described above.

Answered By: Moiz Ali Syed
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.