房價數據分析
數據簡單清洗
data.csv
數據顯示
# 導入模塊
import pandas as pd # 導入數據統計模塊
import matplotlib # 導入圖表模塊
import matplotlib.pyplot as plt # 導入繪圖模塊
# 避免中文亂碼
matplotlib.rcParams['font.sans-serif'] = ['SimHei'] # 設置字體為SimHei顯示中文
matplotlib.rcParams['axes.unicode_minus'] = False # 設置正常顯示字符,使用rc配置文件來自定義
# 簡單清洗
data = pd.read_csv('data.csv') # 讀取csv數據
del data['Unnamed: 0'] # 將索引列刪除
data.dropna(axis=0, how='any', inplace=True) # 刪除data數據中的所有空值
data['單價'] = data['單價'].map(lambda d: d.replace('元/平米', '')) # 將單價“元/平米”去掉
data['單價'] = data['單價'].astype(float) # 將房子單價轉換為浮點類型,float(data['',單價])
data['總價'] = data['總價'].map(lambda d: d.replace('萬', '')) # 將總價“萬”去掉
data['總價'] = data['總價'].astype(float) # 將房子總價轉換為浮點類型,float(data['',單價])
data['建築面積'] = data['建築面積'].map(lambda p: p.replace('平米', '')) # 將建築面積“平米去掉”
data['建築面積'] = data['建築面積'].astype(float) # 將將建築面積轉換為浮點類型
各區均價分析
# 獲取各區二手房均價分析,根據需求,,進一步處理數據,如果要寫相應算法,需要根據算法所需求的數據處理
def get_average_price():
group = data.groupby('區域') # 將房子區域分組
average_price_group = group['單價'].mean() # 計算每個區域的均價,average_price_group字典
x = average_price_group.index # 區域
y = average_price_group.values.astype(int) # 區域對應的均價a =['t':'123'] a.keys()
return x, y # 返回區域與對應的均價,region二關 average_price均價
# 顯示均價條形圖
def average_price_bar(x, y, title):
plt.figure() # 圖形畫布
plt.bar(x, y, alpha=0.8) # 繪制條形圖
plt.xlabel("區域") # 區域文字
plt.ylabel("均價") # 均價文字
plt.title(title) # 表標題文字
# 為每一個圖形加數值標簽
for x, y in enumerate(y):
plt.text(x, y + 100, y, ha='center')
plt.show()
if __name__ == '__main__':
x, y = get_average_price()
title = '各區均價分析'
average_price_bar(x, y, title)
運行如圖
全市二手房裝修程度分析
# 獲取各區二手房均價分析,根據需求,,進一步處理數據,如果要寫相應算法,需要根據算法所需求的數據處理
def get_decorate_sum():
group = data.groupby('裝修') # 將房子區域分組
# decorate_sum_group = group['裝修'].count() # 計算每個區域的均價,average_price_group字典
decorate_sum_group = group.size() # 計算每個區域的均價,average_price_group字典
x = decorate_sum_group.index # 區域
y = decorate_sum_group.values.astype(int) # 區域對應的均價a =['t':'123'] a.keys()
return x, y # 返回區域與對應的均價,region二關 average_price均價
# 顯示均價條形圖
def average_price_bar(x, y, title):
plt.figure() # 圖形畫布
plt.bar(x, y, alpha=0.8) # 繪制條形圖
plt.xlabel("裝修類型") # 區域文字
plt.ylabel("數量") # 均價文字
plt.title(title) # 表標題文字
# 為每一個圖形加數值標簽
for x, y in enumerate(y):
plt.text(x, y + 100, y, ha='center')
plt.show()
if __name__ == '__main__':
x, y = get_decorate_sum()
title = '全市二手房裝修程度分析'
average_price_bar(x, y, title)
各區二手房數量所占比比例
# 獲取各區二手房各區比例數量,進一步處理數據,如果要寫相應算法,需要根據算法所需求的數據處理
def get_proportional_quantity():
area = data['區域'].groupby(data['區域']).count() # 將房子區域分組比例數量
areaName = (area).index.values # 將房子區域分組比例取名
return area, areaName
# 顯示均價條形圖
def proportional_quantity_pie(area, areaName, title):
plt.figure() # 圖形畫布
plt.pie(area, labels=areaName, labeldistance=1.1, autopct='%.1f%%',
shadow=True, startangle=90, pctdistance=0.7)
plt.title(title, fontsize=24) # 表標題文字
plt.legend(bbox_to_anchor=(-0.1, 1)) # 作者標題
plt.show()
if __name__ == '__main__':
# 對應x,y
area, areaName = get_proportional_quantity()
title = '各區二手房數量所占比比例'
proportional_quantity_pie(area, areaName, title)
熱門戶型均價分析
# 獲取各區熱門戶型分析,根據需求,,進一步處理數據,如果要寫相應算法,需要根據算法所需求的數據處理
def get_hot_portal():
# 另外一種方法獲取並取值
"""
group = data.groupby('戶型').size # 將房子區域分組
sort_data = group.sort_values(ascending=False) # 將戶型分組數量進行降序
five_data = sort_data.head() # 提取前5組戶型數據
house_type_mean = data.groupby('戶型')['單價'].mean().astype(int) # 計算每個戶型的均價
x = house_type_mean[five_data.index].index # 戶型
y = house_type_mean[five_data.index].value # 戶型對應的均價
"""
group = data.groupby('戶型') # 將房子區域分組
a = group['戶型'].count().sort_values(ascending=False).head() # 計算每個戶型的均價 字典
b = group['單價'].mean()[a.index] # 區域對應的均價a =['t':'123'] a.keys()
x = b.index
y = b.values.astype(int)
return x, y # 返回區域與對應的均價,region二關 average_price均價
# 顯示均價橫條形圖
def hot_portal_barh(x, y, title):
plt.figure() # 圖形畫布
plt.barh(x, y, alpha=0.9, color='red') # 繪制條形圖
plt.xlabel("均價") # 區域文字
plt.ylabel("戶型") # 均價文字
plt.title(title) # 表標題文字
plt.xlim(0, 15000) # X軸的大小
# 為每一個圖形加數值標簽
for y, x in enumerate(y):
plt.text(x + 100, y, str(x) + '元', ha='left')
plt.show()
if __name__ == '__main__':
x, y = get_hot_portal()
title = '熱門戶型均價分析'
hot_portal_barh(x, y, title)
前面三個圖較簡單,最后相對於前面三個較為麻煩
先獲取得到熱門戶型前五名,通過戶型得到對應的戶型的平均值