交作业之前先写点感言。可以说是千疮百孔,没有第一第二周的突飞猛进,感觉知识很多,又都很生涩,非常不好理解和学习,自己想法很多都实现不了,有种小孩子刚开始学说话,着急表达不出来的感觉,挫败感很强。现在不知道是回头继续学习第三周还是继续先把第四周学完。
还是,学会导入 json 和到处 csv 文件。
import charts
import pymongo
client = pymongo.MongoClient('localhost',27017)
test = client['test']
item_info = test['item_info']
#{'area':[1,2,3]}# 城区是这样一个集合包含着列表的结构
pipeline = [
{'$match':{'$and':[{'pub_date':{'$gte':'2015.12.01','$lte':'2016.01.04'}},{'area':{'$all':['海淀']}}]}},
{'$group':{'_id':{'$slice':['$cates',2,1]},'counts':{'$sum':1}}},
{'$limit':3}
]
for i in item_info.aggregate(pipeline):
print(i)
def deta_gen(date1,date2,area,limit):
pipeline1 = [
{'$match':{'$and':[{'pub_date':{'$gte':date1,'$lte':date2}},{'area':{'$all':area}}]}},
{'$group':{'_id':{'$slice':['$cates',2,1]},'counts':{'$sum':1}}},
{'$limit':limit},
{'$sort':{'counts':-1}}
]
for i in item_info.aggregate(pipeline1):
data = {
'name':i['_id'][0],
'data':[i['counts']],
'type':'column'
}
yield data
for i in deta_gen('2015.12.01','2016.01.04',['海淀'],3):
print(i)
series = [i for i in deta_gen('2015.12.01','2016.01.04',['海淀'],6)]
options = {
'chart':{'zoomType':'xy'},
'title':{'text':'发帖数量最大类目'},
'subtitle':{'text':'数据图表'},
'yAxis':{'title':{'text':'数量'}}
}
charts.plot(series,options=options,show='inline')
pipeline2 = [
{'$match':{'$and':[{'pub_date':{'$gte':'2015.12.01','$lte':'2016.01.04'}},
{'cates':{'$all':['北京二手手机']}},
{'look':{'$nin':['-']}}
]}},
{'$group':{'_id':'$look','avg_price':{'$avg':'$price'}}},
{'$sort':{'avg_price':-1}},
]
for i in item_info.aggregate(pipeline2):
print(i)
def data_gen2(date1,date2,cates):
pipeline = [
{'$match':{'$and':[{'pub_date':{'$gte':date1,'$lte':date2}},
{'cates':{'$all':cates}},
{'look':{'$nin':['-']}}
]}},
{'$group':{'_id':'$look','avg_price':{'$avg':'$price'}}},
{'$sort':{'avg_price':1}}
]
for i in item_info.aggregate(pipeline):
yield i['avg_price']
data = [i for i in data_gen2('2015.12.01','2016.01.04',['北京二手手机'])]
options = {
'title':{'text':'新旧-价格'},
'xAxis':{'categories':['报废机/尸体','7成新及以下','8成新','9成新','95成新','99成新', '全新']},
'yAxis':{'title':{'text':'价格'}}
}
charts.plot(data,options=options,show='inline')