我作为在3D字典数据:最Python的方式来计算平均
movieid, date,customer_id,views
0, (2011,12,22), 0, 22
0, (2011,12,22), 1, 2
0, (2011,12,22), 2, 12
.....
0, (2011,12,22), 7, 2
0, (2011,12,23), 0, 123
.. 所以基本上数据代表了多少次电影已经每天被观看..每个客户(有8个客户)..
现在,我想计算.. 平均一个电影已被每个客户观看了多少次。
所以基本上
movie_id,customer_id, avg_views
0, 0, 33.2
0, 1 , 22.3
and so on
什么是解决这个的Python的方式。
Thakns
编辑:
data = defaultdict(lambda : defaultdict(dict))
date = datetime.datetime(2011,1,22)
data[0][date][0] = 22
print data
defaultdict(<function <lambda> at 0x00000000022F7CF8>,
{0: defaultdict(<type 'dict'>,
{datetime.datetime(2011, 1, 22, 0, 0): {0: 22}}))
假设有只有2客户,1部电影和2天的数据
movie_id, date, customer_id,views
0 , 2011,1,22,0,22
0 , 2011,1,22,1,23
0 , 2011,1,23,0,44
注意:客户1 didnt看了一部电影ID 0日23日1月 现在的答案是
movie_id,customer_id,avg_views
0 , 0 , (22+44)/2
0, 1, (23)/1
请发布(至少一个条目)来保存这些数据的三维字典。 – inspectorG4dget
如果你可以告诉我们你想要的结果如何...... –
你可以格式化你的'defaultdict',以便它是人类可读的吗?如果需要,使用'pprint.pprint'。 – inspectorG4dget