开发工具
python版本:Python 3.6.1
python开发工具:JetBrains PyCharm 2018.3.6 x64
第三方库:pandas ;matplotlib ;seaborn
数据内容
实现代码
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
sns.set_style({'font.sans-serif': ['simhei', 'Arial']})
lianjia_df = pd.read_csv('lianjia.csv')
# 添加房屋均价: 总价/房屋大小
df = lianjia_df.copy()
df['PerPrice'] = round(lianjia_df['Price'] / lianjia_df['Size'], 2)
# 重新摆放列位置
columns = ['Region', 'District', 'Garden', 'Layout', 'Floor', 'Year', 'Size', 'Elevator', 'Direction', 'Renovation',
'PerPrice', 'Price']
df = pd.DataFrame(df, columns=columns)
# 二手房区域分组对比二手房数量和每平米价格
df_house_count = df.groupby('Region')['Price'].count().sort_values(ascending=False).to_frame().reset_index()
df_house_mean = df.groupby('Region')['PerPrice'].mean().sort_values(ascending=False).to_frame().reset_index()
f, [ax1, ax2, ax3] = plt.subplots(3, 1, figsize=(20, 20))
# 每平米单价对比图
sns.barplot(x='Region', y='PerPrice', palette='Blues_d', data=df_house_mean, ax=ax1)
ax1.set_title('北京各大区二手房每平米单价对比', fontsize=15)
ax1.set_xlabel('区域')
ax1.set_ylabel('每平米单价')
# 各区域二手房数量
sns.barplot(x='Region', y='Price', palette='Greens_d', data=df_house_count, ax=ax2)
ax2.set_title('北京各大区二手房数量对比', fontsize=15)
ax2.set_xlabel('区域')
ax2.set_ylabel('数量')
# 各区域二手房房屋总价
sns.boxplot(x='Region', y='Price', data=df, ax=ax3)
ax3.set_title('北京各大区二手房房屋总价', fontsize=15)
ax3.set_xlabel('区域')
ax3.set_ylabel('房屋总价')
plt.show()
运行效果
从第一个区域与每平米单价图中可以看到,西城、东城和海淀地区价格相对较高
从第二个区域与数量图中可以看到,海淀,朝阳,丰台和昌平数量都在2500以上
从第三个区域与总价图中可以看到,总价的平均价格都在1000以下
10000条二手房信息下载地址
https://url71.ctfile.com/f/13238771-530323628-1950bb
(访问密码:8835)
总结
这里主要运用了python的barplot绘制条形图函数和boxplot箱线图函数分析区域与每平米价格、数量和房屋总价的关系