Talk is cheap, how U the code.
源码包含:MongoDB数据加载
Pandas数据处理、提取
Bokeh时间序列曲线,饼图
# coding: utf-8
# In[1]:
import pandas as pd
from bokeh.charts import TimeSeries
from bokeh.io import output_notebook, show
from bokeh.plotting import *
# In[2]:
output_notebook()
# In[3]:
from pymongo import MongoClient,ASCENDING, DESCENDING
con=MongoClient('localhost',27017) #建立连接
db=con.house_price #建立数据库
# In[4]:
cursor = db.price.find({'xiaoqu':'天地源枫林绿洲'})
cursor.count() # 从1月份到现在某网站发布的房源信息条数
# In[5]:
df_cs =pd.DataFrame(list(cursor)) # 生成pandas的数据框架
df=df_cs.copy() # 保存到缓存中
# In[6]:
del df['_id']
# df.index=pd.to_datetime(df['time'].tolist()) # index为时间序列,保留原时间序列
# In[7]:
df_time=df.set_index(['time'])
# df_time
# # 先对df进行数据化清晰,然后可以直接调用高级chart
# In[8]:
# 对数据列进行预处理
f = lambda x: int(x.replace('元/㎡',''))
df_time.danjia = df_time.danjia.map(f)
# df_time.head()
# In[9]:
# 对数据列进行预处理
f = lambda x: float(x.replace('㎡',''))
df_time.mianji = df_time.mianji.map(f)
# df_time.head()
# In[10]:
# 对数据列进行预处理
f = lambda x: x.replace('\n58同城认证商家','')
df_time.zhongjie_gongsi = df_time.zhongjie_gongsi.map(f)
# df_time.head()
# In[11]:
# 对数据列进行预处理
f = lambda x: float(x.replace('万',''))
df_time.zongjia = df_time.zongjia.map(f)
# df_time.head()
# #### 清洗核心数据,看一下格式
# In[12]:
df_time.head()
# In[13]:
df_time.head(1) # 采集的最晚的数据
# In[14]:
df_time.tail(1) # 采集最早的数据
# In[15]:
df_time.shape[0] # 查看截止采集日期的房源发布数量
# In[16]:
df_time.danjia.max() # 单价最高的
# In[17]:
df_time[df_time['danjia']==df_time.danjia.max()]
# In[18]:
df_time.danjia.min() # 单价最低的,这个价位是不是有点注水
# In[19]:
df_time[df_time['danjia']==df_time.danjia.min()]
# In[20]:
df_time.danjia.mean() # 平均单价
# In[21]:
df_danjia = df_time.danjia.resample('M').mean()[:-3]
f = lambda x: '%.2f' %x
df_danjia = df_danjia.map(f)
df_danjia
# In[22]:
data = pd.DataFrame(dict(date=df_danjia.index,
price=df_danjia.values))
data.date[0]
# In[23]:
f = lambda x: x.strftime('%m')+'月'
data.date = data.date.map(f)
# In[24]:
f = lambda x: float(x)
data.price = data.price.map(f)
# In[25]:
data
# In[26]:
from bokeh.charts import TimeSeries,Line, defaults
from bokeh.io import output_notebook, show
output_notebook()
# In[27]:
# 折线图
defaults.plot_width = 450
defaults.plot_height = 400
# In[28]:
line = Line(data,x='date',y='price',xlabel='月份',ylabel='单价',title='2017西安市高新区枫林绿洲房屋单价曲线')
line.title.align= 'center'
show(line)
# In[29]:
ts = TimeSeries(data,x='date',y='price',xlabel='月份',ylabel='单价',title='2017西安市高新区枫林绿洲房屋单价曲线')
ts.title.align= 'center'
show(ts) # 和上面的折线图一样,如果对时间序列进行了str处理,就不能自动缩进x轴(英文显示)
# #### 最近1天内出售的房源,不计重
# In[30]:
df_day = df_time['2017-09-16']
df_day.count()
# In[31]:
df_day.mianji.nunique() # 如此注水,情何以堪
# In[32]:
df_day.tail()
# #### 可见数据做假该有多严重
# ### 看看那家公司注水最多
# In[33]:
df_gs = df_day.zhongjie_gongsi.value_counts()[:6]
df_gs
# In[34]:
from bokeh.charts import Donut
# In[35]:
ts = Donut(df_gs,title='2017西安市高新区枫林中介消息(含水)占比')
ts.title.align= 'center'
show(ts) # 和上面的折线图一样,如果对时间序列进行了str处理,就不能自动缩进x轴(英文显示)
# ### 再看看近一天内的之最数据
# In[36]:
df_day.danjia.max() # 单价之最高
# In[37]:
df_day.danjia.min() # 单价之最低
# In[38]:
df_day.zongjia.max() # 总价之最高
# In[39]:
df_day.zongjia.min() # 总价之最低
# In[40]:
df_day.mianji.max() # 面积之最大
# In[41]:
df_day[df_day['mianji']==df_day.mianji.max()]
# In[42]:
df_day.mianji.min() # 面积之最小
# In[43]:
df_day[df_day['mianji']==df_day.mianji.min()]
# ## 其他
# In[44]:
zhongjije_10 = [i.replace('\n58同城认证商家','')for i in df.zhongjie_gongsi.value_counts()[:10].index]
zhongjije_value = list(df.zhongjie_gongsi.value_counts()[:10].values)
# In[45]:
from bokeh.io import output_notebook, show
# In[46]:
df_danjia_zongjia = df[['danjia','zongjia']].copy()
df_danjia_zongjia.head()
# In[ ]:
注:初探数据之后,如何实现增量数据通过邮件发送给自己?
胶水语言博大精深,
本主只得一二为新人带路:yeayee:Python数据分析及可视化实例目录zhuanlan.zhihu.com
最后,别只收藏不关注哈