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] :
Paste_Image.png
4. 總結
- mongodb
update()
方法:
update() 方法用于更新已存在的文檔。語法格式如下:
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)
參數說明:
- **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圖表的任何需求 !