You might want to give pandas a try, which is an excellent python data analysis library.
To query a PostgreSQL database:
import psycopg2
import pandas as pd
from pandas.io.sql import frame_query
conn_string = "host='localhost' dbname='mydb' user='postgres' password='password'"
conn = psycopg2.connect(conn_string)
df = frame_query('select * from cities', con=conn)
Where df
is a DataFrame like:
city state
0 1 1
1 2 2
2 3 1
3 4 1
You can then create a pivot table using pivot_table
and dividing by the total to get percentages:
totals = df.groupby('state').size()
pivot = pd.pivot_table(df, rows='city', cols='state', aggfunc=len, fill_value=0) / totals
Giving you the result:
state 1 2
city
1 0.333333 0
2 0 1
3 0.333333 0
4 0.333333 0
Finally to get the layout you want, you just need to rename the index and columns, and append the totals:
totals_frame = pd.DataFrame(totals).T
totals_frame.index = ['totalCount']
pivot.index = ['city%i' % item for item in pivot.index]
final_result = pivot.append(totals_frame)
final_result.columns = ['state-%i' % item for item in final_result.columns]
Giving you:
state-1 state-2
city1 0.333333 0
city2 0.000000 1
city3 0.333333 0
city4 0.333333 0
totalCount 3.000000 1