Postgresql Window Function Count In Python For Loop