3-总




Untitled6















    
    
    
    

  
In [1]:
import charts
import pymongo
Server running in the folder /Users/Hou at 127.0.0.1:57140
In [13]:
client = pymongo.MongoClient('localhost', 27017)
ceshi = client['ceshi']
item_info = ceshi['item_infoS']

问题:某段时间内,北京各个城区发帖数量的 top3 类目

已知:

  1. 某个时间段,例:2015.12.25~2015.12.27
  2. 北京各个城区,例:朝阳

求解:

  1. 发帖数量 top3

所需数据结构:

  1. 原始 :series = [{'name': 'name','data': [100]},{'name': 'name','data': [100]}]
  2. 实际上:{name:类目,data:发帖量}
  3. 目标:{'_id': ['北京二手家电'], 'counts': 175}
In [25]:
series = [
    {
    'name': 'name',
    'data': [100],
    'type': 'column'
},{
    'name': 'name2',
    'data': [102],
    'type': 'column'
}]
    

options = {
    'chart'   : {'zoomType':'xy'},
    'title'   : {'text': '发帖数量最大的类目'},
    'subtitle': {'text': '数据图表'},
    'yAxis'   : {'title': {'text': '数量'}}
    }

charts.plot(series,options=options,show='inline')
Out[25]:

Adjust chart settings

.json
In [33]:
# {'area':[1,2,3]}
pipeline1 = [
    {'$match':{'$and':[{'pub_date':{'$gte':'2015.12.25','$lte':'2015.12.27'}},{'area':{'$all':['朝阳']}}]}},
    {'$group':{'_id':{'$slice':['$cates',2,1]},'counts':{'$sum':1}}},
    {'$limit':3}
]

for i in item_info.aggregate(pipeline1):
    print(i)
{'_id': ['北京二手图书/音像/软件'], 'counts': 32}
{'_id': ['北京其他二手物品'], 'counts': 26}
{'_id': ['北京二手办公用品/设备'], 'counts': 86}
In [37]:
def data_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
In [35]:
for i in data_gen('2015.12.25','2015.12.27',['朝阳'],3):
    print(i)
{'name': '北京二手图书/音像/软件', 'type': 'column', 'data': [32]}
{'name': '北京其他二手物品', 'type': 'column', 'data': [26]}
{'name': '北京二手办公用品/设备', 'type': 'column', 'data': [86]}
In [39]:
series = [i for i in data_gen('2015.12.25','2015.12.27',['朝阳'],5)]
options = {
    'chart'   : {'zoomType':'xy'},
    'title'   : {'text': '发帖数量最大的类目'},
    'subtitle': {'text': '数据图表'},
    'yAxis'   : {'title': {'text': '数量'}}
    }

charts.plot(series,options=options,show='inline')
Out[39]:

Adjust chart settings

.json
In [ ]:
 

问题:某段时间内各大类目中成色对应的平均价格

已知:

  1. 某个时间段,例:2015.12.25~2015.12.27
  2. 各大类目,例:北京二手手机
  3. 单个物品价格,例:某品牌二手电视,价格1000元

求解:

  1. 平均价格

所需数据结构:

  1. 原始:series = ['data1','data2','data3']
  2. 实际上:[全新对应的价格,9成新对应的价格,8成新对应的价格,7成新对应的价格]
  3. 目标:{'avg_price': 300.0, '_id': '7成新及以下'}

In [28]:
data = [1,2,3,4,5,6,7]
options = {
    'title': {'text': '新旧-价格'},
    'xAxis'   : {'categories': ['报废机/尸体','7成新及以下','8成新','9成新','95成新','99成新', '全新']},
    'yAxis'   : {'title': {'text': '价格'}},
    
}

charts.plot(data,show='inline', options=options)
Out[28]:

Adjust chart settings

.json
In [40]:
for i in item_info.find({},{'_id':0,'look':1}).limit(100):
    print(i)
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '9成新'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '95成新'}
{'look': '-'}
{'look': '9成新'}
{'look': '8成新'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '8成新'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '95成新'}
{'look': '-'}
{'look': '-'}
{'look': '7成新及以下'}
{'look': '9成新'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '8成新'}
{'look': '9成新'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '9成新'}
{'look': '95成新'}
{'look': '-'}
{'look': '8成新'}
{'look': '9成新'}
{'look': '95成新'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '9成新'}
{'look': '-'}
{'look': '-'}
{'look': '全新'}
{'look': '-'}
{'look': '9成新'}
{'look': '8成新'}
{'look': '-'}
{'look': '-'}
{'look': '9成新'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '95成新'}
{'look': '9成新'}
{'look': '9成新'}
{'look': '-'}
{'look': '-'}
{'look': '95成新'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '9成新'}
{'look': '-'}
{'look': '-'}
{'look': '8成新'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '8成新'}
{'look': '-'}
{'look': '-'}
{'look': '-'}
{'look': '95成新'}
{'look': '7成新及以下'}
{'look': '-'}
{'look': '-'}
In [50]:
pipeline2 = [
    {'$match':{'$and':[{'pub_date':{'$gte':'2015.12.25','$lte':'2015.12.27'}},
                       {'cates':{'$all':['北京二手手机']}},
                       {'look':{'$nin':['-']}}
                      ]}},
    {'$group':{'_id':'$look','avg_price':{'$avg':'$price'}}},
    {'$sort':{'avg_price':-1}}
]
In [51]:
for i in item_info.aggregate(pipeline2):
    print(i)
{'avg_price': 1849.7894736842106, '_id': '全新'}
{'avg_price': 1704.5, '_id': '99成新'}
{'avg_price': 1626.923076923077, '_id': '95成新'}
{'avg_price': 1187.5151515151515, '_id': '9成新'}
{'avg_price': 730.0, '_id': '8成新'}
{'avg_price': 300.0, '_id': '7成新及以下'}
{'avg_price': 220.0, '_id': '报废机/尸体'}
In [53]:
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']
In [55]:
data = [i for i in data_gen2('2015.12.24','2016.01.10',['北京二手手机'])]
options = {
    'title': {'text': '新旧-价格'},
    'xAxis'   : {'categories': ['报废机/尸体','7成新及以下','8成新','9成新','95成新','99成新', '全新']},
    'yAxis'   : {'title': {'text': '价格'}},
    
}

charts.plot(data,show='inline', options=options)
Out[55]:

Adjust chart settings

.json
In [ ]:
 

如何导出 csv ?

mongoexport -d database -c collection -o output/path.csv

mongoexport -d ceshi -c item_infoZ -o Users/Hou/Desktop/ddd.json

In [ ]:
 

你可能感兴趣的:(3-总)