第三周/第二节学习项目: 绘制各区域对比柱状图

1. 引言

统计赶集网-上海-二手市场19个大类目的发帖量, 并在jupyter-notebook中绘制出各区域发帖量对比的柱状图

2. 分析

  • 先整理源数据, 商品区域信息为None的替换成火星那旮旯
  • 筛选重复的区域使之唯一
  • 分别统计区域出现的次数
  • 生成合乎charts要求格式的字典列表

3. 实现

In [1] :
from pymongo import MongoClient
from string import punctuation
import charts

Server running in the folder /home/wjh at 127.0.0.1:53200


In [2] :
client = MongoClient('10.66.17.17', 27017)
database = client['ganji']
item_info_collection = database['sh_ershou_itemY']

In [3] :
# 修改表中区域为空及二级区域为空的条目
for item in item_info_collection.find():
        # 区域不为空
        if item['area']:
            # 二级区域不为空, 则区域不需要修改
            if item['area'][1]:
                area = item['area']
            # 二级区域为空, 则整个区域换成 原一级区域+'旮旯'
            else:
                area = [i for i in (item['area'][0], '旮旯')]
        # 区域为空, 则替换成: '火星'+'旮旯'
        else:
            area = ['火星', '旮旯']
        # 将区域逐一替换成已经修改好的area
        item_info_collection.update_one({'_id': item['_id']}, {'$set': {'area': area}})
# 输出看下是什么结果
[i['area'] for i in item_info_collection.find().limit(100)]
Out [3] :
[['上海', '徐汇', '植物园'],
 ['火星', '旮旯'],
 ['上海', '松江', '新桥'],
 ['上海', '宝山', '罗南'],
 ['上海', '旮旯'],
 ['火星', '旮旯'],
 ['上海', '浦东'],
 ['火星', '旮旯'],
 ['上海', '浦东', '曹路'],
 ['上海', '浦东', '合庆'],
 ['上海', '松江', '松江大学城'],
 ['上海', '嘉定', '马陆'],
 ['火星', '旮旯'],
...]

In [4] :
# 包含所有二级区域的列表
area_list = [i['area'][1] for i in item_info_collection.find()]
# 区域名字是唯一的集合
area_set = set(item_list)
# 输出看下是什么结果
print(len(area_set), area_set)

21 {'金山', '长宁', '卢湾', '青浦', '杨浦', '普陀', '嘉定', '上海周边', '旮旯', '徐汇', '崇明', '静安', '黄浦', '宝山', '奉贤', '闵行', '浦东', '南汇', '虹口', '闸北', '松江'}


In [5] :
# 统计区域出现次数的列表, 如下看到有21个区域, 包含火星那旮旯
area_times = [area_list.count(index) for index in area_set]
# 输出看下是什么结果
print(len(area_times), area_times)

21 [357, 1581, 486, 1100, 2336, 2817, 2676, 861, 2302, 2848, 90, 1063, 1172, 3222, 1045, 6229, 10469, 827, 1560, 1611, 3174]


In [6] :
# 定义生成图表数据的函数
def area_data_gen(types):
    length = 0
    # 循环次数为区域集合长度
    if length <= len(area_set):
        for name, time in zip(area_set, area_times):
            data = {
                'name': name,
                'data': [time],
                'type': types,
            }
            # 遇到yield语句返回,再次执行时从上次返回的yield语句处继续执行, 所以循环执行就有一个字典列表了
            yield data
# 输出看下是什么结果
[i for i in area_data_gen('column')]
Out [6] :
[{'data': [357], 'name': '金山', 'type': 'column'},
 {'data': [1581], 'name': '长宁', 'type': 'column'},
 {'data': [486], 'name': '卢湾', 'type': 'column'},
 {'data': [1100], 'name': '青浦', 'type': 'column'},
 {'data': [2336], 'name': '杨浦', 'type': 'column'},
 {'data': [2817], 'name': '普陀', 'type': 'column'},
 {'data': [2676], 'name': '嘉定', 'type': 'column'},
 {'data': [861], 'name': '上海周边', 'type': 'column'},
 {'data': [2302], 'name': '旮旯', 'type': 'column'},
 {'data': [2848], 'name': '徐汇', 'type': 'column'},
 {'data': [90], 'name': '崇明', 'type': 'column'},
 {'data': [1063], 'name': '静安', 'type': 'column'},
 {'data': [1172], 'name': '黄浦', 'type': 'column'},
 {'data': [3222], 'name': '宝山', 'type': 'column'},
 {'data': [1045], 'name': '奉贤', 'type': 'column'},
 {'data': [6229], 'name': '闵行', 'type': 'column'},
 {'data': [10469], 'name': '浦东', 'type': 'column'},
 {'data': [827], 'name': '南汇', 'type': 'column'},
 {'data': [1560], 'name': '虹口', 'type': 'column'},
 {'data': [1611], 'name': '闸北', 'type': 'column'},
 {'data': [3174], 'name': '松江', 'type': 'column'}]

In [7] :
# 生成数据
serises = [i for i in area_data_gen('column')]
# 传入参数并绘制图表
charts.plot(serises, show='inline', options=dict(title=dict(text='近段时间上海城区二手物品发帖量')))
Out [7] :
第三周/第二节学习项目: 绘制各区域对比柱状图_第1张图片
Paste_Image.png

4. 总结

  • mongodb update() 方法:
    update() 方法用于更新已存在的文档。语法格式如下:
db.collection.update(
   ,
   ,
   {
     upsert: ,
     multi: ,
     writeConcern: 
   }
)

参数说明:

  • **query **: update的查询条件,类似sql update查询内where后面的。
  • **update **: update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
  • **upsert **: 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
  • **multi **: 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
  • **writeConcern **:可选,抛出异常的级别。
    实例
    只更新第一条记录:
db.col.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } );

全部更新:

db.col.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true );

只添加第一条:

db.col.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false );

全部添加加进去:

db.col.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true );

全部更新:

db.col.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );

只更新第一条记录:

db.col.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );

  • highcharts:
    Highcharts是一款纯javascript编写的图表库,能够很简单便捷的在Web网站或Web应用中添加交互性的图表,Highcharts目前支持直线图、曲线图、面积图、柱状图、饼图、散点图等多达18种不同类型的图表,可以满足你对Web图表的任何需求 !

你可能感兴趣的:(第三周/第二节学习项目: 绘制各区域对比柱状图)