前言
嗨嘍~大家好呀,這里是魔王吶 ? ~!
今天我們的目的想必大家看標題就能明白了~
準備
首先,我們要提前準備好數(shù)據(jù)
然后打開我們的數(shù)據(jù)分析工具: Jupyter
代碼及效果展示
導入模塊
# 導入做數(shù)據(jù)處理的模塊pandas
import pandas as pd
# 導入繪圖模塊pyecharts,*號代表模塊里面的所有圖形
from pyecharts.charts import *
from pyecharts import options as opts
數(shù)據(jù)處理
1.讀取數(shù)據(jù)
導入數(shù)據(jù)
設置編碼encoding='gbk'
設置解釋器為engine='python'
df = pd.read_csv('二手房數(shù)據(jù).csv', encoding='gbk', engine='python')
df
2.查看表格數(shù)據(jù)描述
describe
可以直接計算數(shù)值類型數(shù)據(jù)的平均值,標準差
df.describe()
3.查看表格是否有數(shù)據(jù)缺失
通過isnul
l查找出包含缺失值的字段
然后進行求和,計算每一列的缺失數(shù)據(jù)的數(shù)量
df.isnull().sum()
可以看到電梯數(shù)據(jù)缺失8257行
4.查看電梯列共有幾種值
通過unique
可以統(tǒng)計數(shù)據(jù)里面出現(xiàn)了幾種值
方便后面進行填充
df['電梯'].unique()
5.缺失值填充
用“未知”填充缺失值
缺失值的處理方式有兩種
第一種刪除,第二種填補
-
缺失值少,就直接刪除
-
缺失值多,就進行填補
這里缺失值占比較多,所以進行填補
df['電梯'].fillna('未知',inplace=True)
# 填補之后查看是否還有缺失值
df.isnull().sum()
6.查看房屋朝向數(shù)據(jù)
查看朝向值的種類
df['朝向'].unique()
朝向數(shù)據(jù)包含了‘西南’和‘南西’兩個方向,將其合并為一個方向‘西南’:
# replace(被替換的值,替換后的值):
df['朝向'] = df['朝向'].str.replace('南西','西南')
df['朝向'].unique()
可以看到,豐臺、朝陽、海淀、昌平在售的房源數(shù)量最多,高達12000多套,占了總量的1/2
?? 博主文章素材、解答、源碼、教程領(lǐng)取處:點擊
Pyecharts可視化
1.統(tǒng)計各城區(qū)二手房數(shù)據(jù)
# 這里我們要用到分組
g = df.groupby('市區(qū)')
df_region = g.count()['小區(qū)']
region = df_region.index.tolist()
count = df_region.values.tolist()
df_region
1.各城區(qū)二手房數(shù)量北京市地圖
# 各城區(qū)二手房數(shù)量北京市地圖
new = [x + '區(qū)' for x in region]
m = (
Map()
.add('北京市', [list(z) for z in zip(new, count)], '北京')
.set_global_opts(
title_opts=opts.TitleOpts(title='北京市二手房各區(qū)分布'),
visualmap_opts=opts.VisualMapOpts(max_=3000),
)
)
m.render_notebook()
2.各城區(qū)二手房數(shù)量-平均價格柱狀圖
df_price = g.mean()['價格(萬元)']
df_price = round(df_price,2)
price = df_price.values.tolist()
bar = (
Bar()
.add_xaxis(region)
.add_yaxis('數(shù)量',count,
label_opts=opts.LabelOpts(is_show=True)
)
.extend_axis(
yaxis=opts.AxisOpts(
name='價格(萬元)',
type_='value',
min_=200,
max_=900,
interval=100,
axislabel_opts=opts.LabelOpts(formatter='{value}')
)
)
.set_global_opts(
title_opts=opts.TitleOpts(title='各城區(qū)二手房數(shù)量-平均價格柱狀圖'),
tooltip_opts=opts.TooltipOpts(
is_show=True, trigger="axis", axis_pointer_type="cross"
),
xaxis_opts=opts.AxisOpts(
type_="category",
axispointer_opts=opts.AxisPointerOpts(is_show=True, type_="shadow"),
),
yaxis_opts=opts.AxisOpts(name='數(shù)量',
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=False),)
)
)
line2 = (
Line()
.add_xaxis(xaxis_data=region)
.add_yaxis(
series_name="價格",
yaxis_index=1,
y_axis=price,
label_opts=opts.LabelOpts(is_show=True),
z=10
)
)
bar.overlap(line2)
grid = Grid()
grid.add(bar, opts.GridOpts(pos_left="5%", pos_right="20%"), is_control_axis_index=True)
grid.render_notebook()
3.二手房價格最低Top15
top_price = df.sort_values(by='價格(萬元)')[:15]
top_price
area0 = top_price['小區(qū)'].values.tolist()
count = top_price['價格(萬元)'].values.tolist()
bar = (
Bar()
.add_xaxis(area0)
.add_yaxis('數(shù)量', count, category_gap='50%')
.set_global_opts(
yaxis_opts=opts.AxisOpts(name='價格(萬元)'),
xaxis_opts=opts.AxisOpts(name='數(shù)量'),
datazoom_opts=opts.DataZoomOpts(type_='slider')
)
)
bar.render_notebook()
4 二手房總價與面積散點圖
s = (
Scatter()
.add_xaxis(df['面積(㎡)'].values.tolist())
.add_yaxis('',df['價格(萬元)'].values.tolist())
.set_global_opts(xaxis_opts=opts.AxisOpts(type_='value'))
)
s.render_notebook()
5.房屋朝向餅圖
g = df.groupby('朝向')
g.count()['小區(qū)']
df_direction = g.count()['小區(qū)']
df_direction
directions = df_direction.index.tolist()
count = df_direction.values.tolist()
c1 = (
Pie(init_opts=opts.InitOpts(
width='800px', height='600px',
)
)
.add(
'',
[list(z) for z in zip(directions, count)],
radius=['20%', '60%'],
center=['40%', '50%'],
# rosetype="radius",
label_opts=opts.LabelOpts(is_show=True),
)
.set_global_opts(title_opts=opts.TitleOpts(title='房屋朝向占比',pos_left='33%',pos_top="5%"),
legend_opts=opts.LegendOpts(type_="scroll", pos_left="80%",pos_top="25%",orient="vertical")
)
.set_series_opts(label_opts=opts.LabelOpts(formatter=':{c} (n5n3t3z%)'),position="outside")
)
c1.render_notebook()
6.裝修情況/有無電梯玫瑰圖
g1 = df.groupby('裝修情況')
g1.count()['小區(qū)']
g2 = df.groupby('電梯')
g2.count()['小區(qū)']
df_fitment = g1.count()['小區(qū)']
df_direction = g2.count()['小區(qū)']
df_fitment
fitment = df_fitment.index.tolist()
count1 = df_fitment.values.tolist()
directions = df_direction.index.tolist()
count2 = df_direction.values.tolist()
bar = (
Bar()
.add_xaxis(fitment)
.add_yaxis('', count1, category_gap = '50%')
.reversal_axis()
.set_series_opts(label_opts=opts.LabelOpts(position='right'))
.set_global_opts(
xaxis_opts=opts.AxisOpts(name='數(shù)量'),
title_opts=opts.TitleOpts(title='裝修情況/有無電梯玫瑰圖(組合圖)',pos_left='33%',pos_top="5%"),
legend_opts=opts.LegendOpts(type_="scroll", pos_left="90%",pos_top="58%",orient="vertical")
)
)
c2 = (
Pie(init_opts=opts.InitOpts(
width='800px', height='600px',
)
)
.add(
'',
[list(z) for z in zip(directions, count2)],
radius=['10%', '30%'],
center=['75%', '65%'],
rosetype="radius",
label_opts=opts.LabelOpts(is_show=True),
)
.set_global_opts(title_opts=opts.TitleOpts(title='有/無電梯',pos_left='33%',pos_top="5%"),
legend_opts=opts.LegendOpts(type_="scroll", pos_left="90%",pos_top="15%",orient="vertical")
)
.set_series_opts(label_opts=opts.LabelOpts(formatter=':{c} \n (n5n3t3z%)'),position="outside")
)
bar.overlap(c2)
bar.render_notebook()
7.二手房樓層分布柱狀圖
g = df.groupby('樓層')
df_floor = g.count()['小區(qū)']
df_floor
floor = df_floor.index.tolist()
count = df_floor.values.tolist()
bar = (
Bar()
.add_xaxis(floor)
.add_yaxis('數(shù)量', count)
.set_global_opts(
title_opts=opts.TitleOpts(title='二手房樓層分布柱狀縮放圖'),
yaxis_opts=opts.AxisOpts(name='數(shù)量'),
xaxis_opts=opts.AxisOpts(name='樓層'),
datazoom_opts=opts.DataZoomOpts(type_='slider')
)
)
bar.render_notebook()
8.房屋面積分布柱狀圖
area_level = [0, 50, 100, 150, 200, 250, 300, 350, 400, 1500]
label_level = ['小于50', '50-100', '100-150', '150-200', '200-250', '250-300', '300-350', '350-400', '大于400']
jzmj_cut = pd.cut(df['面積(㎡)'], area_level, labels=label_level)
df_area = jzmj_cut.value_counts()
df_area
area = df_area.index.tolist()
count = df_area.values.tolist()
bar = (
Bar()
.add_xaxis(area)
.add_yaxis('數(shù)量', count)
.reversal_axis()
.set_series_opts(label_opts=opts.LabelOpts(position="right"))
.set_global_opts(
title_opts=opts.TitleOpts(title='房屋面積分布縱向柱狀圖'),
yaxis_opts=opts.AxisOpts(name='面積(㎡)'),
xaxis_opts=opts.AxisOpts(name='數(shù)量'),
)
)
bar.render_notebook()
結(jié)論
豐臺、朝陽、海淀、昌平在售的房源數(shù)量最多,高達12000多套,占了總量的1/2。
東城區(qū)、西城區(qū)和海淀區(qū)二手房平均售價最高,均在800萬元以上。
二手房面積多集中于0-400平米,價格在0-3000萬元居多。
房屋朝向約50%是坐北朝南的
推薦往期文章
?? 博主所有文章素材、解答、源碼、教程領(lǐng)取處:點擊
對python感興趣的小伙伴也可以看一下博主其他相關(guān)文章哦~
python小介紹:
python是什么?工作前景如何?怎么算有基礎(chǔ)?爬數(shù)據(jù)違法嘛?。。
python數(shù)據(jù)分析前景:
用python分析“數(shù)據(jù)分析”到底值不值得學習,以及學完之后大概能拿到多少工資
python基礎(chǔ)自測題:
Python 800 道習題 (°ー°〃) 測試你學廢了嘛
最后推薦一套Python視頻給大家,希望對大家有所幫助:
全套教程!你和大佬只有一步之遙【python教程】
尾語
要成功,先發(fā)瘋,下定決心往前沖!
學習是需要長期堅持的,一步一個腳印地走向未來!
未來的你一定會感謝今天學習的你。
—— 心靈雞湯
本文章到這里就結(jié)束啦~感興趣的小伙伴可以復制代碼去試試哦 ??文章來源:http://www.zghlxwxcb.cn/news/detail-495620.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-495620.html
到了這里,關(guān)于python帶你對北京二手房進行數(shù)據(jù)分析,看看大概都什么價位的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!