NOTE. Look for some help on an efficient way to do this, other than a mega connection, and then figure out the difference between dates
I have table1 with country identifier and date (without duplicates of these values), and I want to summarize the information of table2 (which has the country, date, cluster_x and variable count, where cluster_x is cluster_1, cluster_2, cluster_3), so table1 added to each value of the cluster identifier and the total score from table2 , where the date from table2 occurred within 30 days to the date in table1 .
I find it easy in SQL: how to do it in Pandas?
select a.date,a.country, sum(case when a.date - b.date between 1 and 30 then b.cluster_1 else 0 end) as cluster1, sum(case when a.date - b.date between 1 and 30 then b.cluster_2 else 0 end) as cluster2, sum(case when a.date - b.date between 1 and 30 then b.cluster_3 else 0 end) as cluster3 from table1 a left outer join table2 b on a.country=b.country group by a.date,a.country
EDIT:
Here is a slightly modified example. Let's say this is table1, an aggregate data set with a date, city, cluster, and counter. Below is a query data set (table 2). in this case, we want to summarize the count field from table1 for cluster1, cluster2, cluster3 (actually 100 of them), corresponding to the country identifier, if the date field in table 1 is 30 days before.
So, for example, the first row of the query dataset has the date 2/2/2015 and country 1. In table 1 there is only one row 30 days before, and this is for cluster 2 with the score 2.

Here is a dump of two tables in a CSV:
date,country,cluster,count 2014-01-30,1,1,1 2015-02-03,1,1,3 2015-01-30,1,2,2 2015-04-15,1,2,5 2015-03-01,2,1,6 2015-07-01,2,2,4 2015-01-31,2,3,8 2015-01-21,2,1,2 2015-01-21,2,1,3
and table2:
date,country 2015-02-01,1 2015-04-21,1 2015-02-21,2