Quiero sumar dos arrays , follow_dismiss
y display
de tamaño differente que vienene de consultas SQL que tienen un valor y un index sobre los index que vienene de una consulta sql :
cur.execute("""-- nombre de fois ou l'utilisateur fait ce qu'on attend de lui par utilisateur
SELECT COUNT (swipe.state),swipe.subscriber_id FROM swipe
WHERE swipe.state= 2 OR swipe.state = 3 or swipe.state=5
OR swipe.state =6 or swipe.state=8 or swipe.state=9
GROUP BY swipe.subscriber_id
ORDER BY swipe.subscriber_id;""")
follow_dismiss = cur.fetchall()
print "follow_dismiss"
y
cur.execute("""-- nombre de fois ou l'on propose une pub a l'utilisateur par utilisateur
SELECT COUNT (swipe.state),swipe.subscriber_id FROM swipe
WHERE swipe.state= 1 OR swipe.state = 4 or swipe.state=7
GROUP BY swipe.subscriber_id
ORDER BY swipe.subscriber_id;""")
dan :
follow_dismiss
[(409L, 13), (14L, 54), (59L, 68), (6L, 150), (32L, 152), (62L, 156), (59L, 158), (120L, 160), (53L, 161), (150L, 162), (3L, 169), (1L, 171), (60L, 172), (1L, 177), (126L, 179), (41L, 185), (239L, 189), (163L, 190), (11L, 216), (42L, 223), (1L, 272), (2L, 286), (5L, 289), (1L, 292), (2L, 294), (4L, 296)]
display
[(384L, 13), (6L, 54), (300L, 68), (1L, 150), (8L, 152), (33L, 156), (30L, 158), (24L, 160), (5L, 161), (233L, 162), (1L, 171), (65L, 172), (21L, 179), (20L, 185), (86L, 189), (171L, 190), (2L, 216), (29L, 223), (2L, 276), (3L, 286), (1L, 296)]
Quiero obtener :
[(793,13), (20,54),...]
A qui esta lo que he intentado :
follow_dismiss=[(409L, 13), (14L, 54), (59L, 68), (6L, 150), (32L, 152), (62L, 156), (59L, 158), (120L, 160), (53L, 161), (150L, 162), (3L, 169), (1L, 171), (60L, 172), (1L, 177), (126L, 179), (41L, 185), (239L, 189), (163L, 190), (11L, 216), (42L, 223), (1L, 272), (2L, 286), (5L, 289), (1L, 292), (2L, 294), (4L, 296)]
display=[(384L, 13), (6L, 54), (300L, 68), (1L, 150), (8L, 152), (33L, 156), (30L, 158), (24L, 160), (5L, 161), (233L, 162), (1L, 171), (65L, 172), (21L, 179), (20L, 185), (86L, 189), (171L, 190), (2L, 216), (29L, 223), (2L, 276), (3L, 286), (1L, 296)]
def column_sums(a, b):
result = []
for i in range(len(a)):
for j in range(len(b)):
if a[i][2]=b[j][2]
result[i][1] =a[i][1]+b[j][1]
else if a[i][2]<b[j][2]
result[i][1]=a[i][1]
result[i][2]= a[j][2]
j = j-1 # para quedar
else if a[i][2]>b[j][2]
result[i][1]= b[j][1]
result[i][2]= b[j][2]
i = i-1 # para quedar
a = colums_sums(follow_dismiss,display)
print a
Pero me dice :
./testPython.py: line 1: syntax error near unexpected token `('
./testPython.py: line 1: `follow_dismiss=[(409L, 13), (14L, 54), (59L, 68), (6L, 150), (32L, 152), (62L, 156), (59L, 158), (120L, 160), (53L, 161), (150L, 162), (3L, 169), (1L, 171), (60L, 172), (1L, 177), (126L, 179), (41L, 185), (239L, 189), (163L, 190), (11L, 216), (42L, 223), (1L, 272), (2L, 286), (5L, 289), (1L, 292), (2L, 294), (4L, 296)]'
Actualizacion 23/05/2017
Hizo con la ayuda de la respuesta de FJSevilla :
cur.execute("""-- nombre de fois ou l'utilisateur fait ce qu'on attend de lui par utilisateur
SELECT COUNT (swipe.state),swipe.subscriber_id FROM swipe
WHERE swipe.state= 2 OR swipe.state = 3 or swipe.state=5
OR swipe.state =6 or swipe.state=8 or swipe.state=9
GROUP BY swipe.subscriber_id
ORDER BY swipe.subscriber_id;""")
follow_dismiss_sql = cur.fetchall()
cur.execute("""-- nombre de fois ou l'on propose une pub a l'utilisateur par utilisateur
SELECT COUNT (swipe.state),swipe.subscriber_id FROM swipe
WHERE swipe.state= 1 OR swipe.state = 4 or swipe.state=7
GROUP BY swipe.subscriber_id
ORDER BY swipe.subscriber_id;""")
display_sql = cur.fetchall()
follow_dismiss = np.array(follow_dismiss_sql)
display = np.array(display_sql)
print follow_dismiss_sql
print display_sql
df = pd.DataFrame(follow_dismiss+display, columns = ('counts', 'id') )
suma = df.groupby("id").sum()
out = pd.np.column_stack((suma.counts.values, suma.index.values))
print(out)
df = pd.DataFrame(follow_dismiss+display, columns = ('counts', 'id') )
suma = df.groupby("id").sum()
out = pd.np.column_stack((suma.counts.values, suma.index.values))
print(out)
Pero me dice que :
df = pd.DataFrame(follow_dismiss+display, columns = ('counts', 'id') )
ValueError: operands could not be broadcast together with shapes (26,2) (21,2)
Pero funciona cuando hizo la respuesta de FJSevilla ...
np.array(sql_query)
. Le mostraré en la actualización ;) Pero ya no se como hacer la suma sobre los index.