0
我有一些稱爲約束矩陣,其中每個用戶(muid)可以發送一個內容(策略)最大N次。因此,我已表示它作爲一個數據幀爲:自動條件來檢查一個上閾值
tactic max_times
100 50
101 35
102 23
103 30
...........
.........
命名100只有最大的50倍,101最大的35倍等,從而每個用戶可以發送策略。
現在我們必須模擬每個用戶在各種迭代中發送特定內容(策略)。
## code to simulate data
import random
muid=range(1,11)
tactic=range(100,110)
##create dataframe for first iteration
temp=DataFrame({'muid':muid,'tactic':tactic})
print temp
muid tactic
0 1 100
1 2 101
2 3 102
3 4 103
4 5 104
##reshuffle the sent channel
tactic1=random.sample(range(100,110),10)
##dataframe for second iteration
temp1=DataFrame({'muid':muid,'tactic':tactic1})
print temp1
muid tactic
0 1 101
1 2 100
2 3 106
3 4 107
4 5 109
5 6 102
6 7 104
7 8 108
8 9 105
9 10 103
###code to calculate how many times user has been sent/exposed to a channel
#groupby for first iteration
groupedby=temp.groupby(['muid','tactic'])['tactic'].agg({'count':'count'})
groupedby.reset_index(inplace=True)
#groupby for second iteration
groupedby1=temp1.groupby(['muid','tactic'])['tactic'].agg({'count':'count'})
groupedby1.reset_index(inplace=True)
appended_output=groupedby.append(groupedby1)
total_counts_at_second_iteration=appended_output.groupby(['muid','tactic'])['tactic'].agg({'total_sent':'count'})
所以在第二次迭代之後的端輸出如下所示:
##top 8 rows
muid tactic total_sent
0 1 100 1
1 1 101 1
2 2 101 1
3 2 107 1
4 3 102 1
5 3 104 1
6 4 103 2
7 5 102 1
8 5 104 1
上述輸出對應於2次迭代。現在我想把上面的代碼放在循環中(N次迭代),並且一旦達到任何發送策略的約束,該策略就不應該再發送,並且應該顯示一條消息「策略達到的限制」。任何幫助,應該感激。