詳解Python對某地區(qū)二手房房價數(shù)據(jù)分析
更新時間:2021年12月03日 15:50:46 作者:李好秀
這篇文章主要為大家介紹了Python數(shù)據(jù)分析某地區(qū)二手房房價,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
房價數(shù)據(jù)分析
數(shù)據(jù)簡單清洗
data.csv
數(shù)據(jù)顯示
# 導入模塊 import pandas as pd # 導入數(shù)據(jù)統(tǒng)計模塊 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數(shù)據(jù) del data['Unnamed: 0'] # 將索引列刪除 data.dropna(axis=0, how='any', inplace=True) # 刪除data數(shù)據(jù)中的所有空值 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) # 將將建筑面積轉換為浮點類型
各區(qū)均價分析
# 獲取各區(qū)二手房均價分析,根據(jù)需求,,進一步處理數(shù)據(jù),如果要寫相應算法,需要根據(jù)算法所需求的數(shù)據(jù)處理 def get_average_price(): group = data.groupby('區(qū)域') # 將房子區(qū)域分組 average_price_group = group['單價'].mean() # 計算每個區(qū)域的均價,average_price_group字典 x = average_price_group.index # 區(qū)域 y = average_price_group.values.astype(int) # 區(qū)域對應的均價a =['t':'123'] a.keys() return x, y # 返回區(qū)域與對應的均價,region二關 average_price均價 # 顯示均價條形圖 def average_price_bar(x, y, title): plt.figure() # 圖形畫布 plt.bar(x, y, alpha=0.8) # 繪制條形圖 plt.xlabel("區(qū)域") # 區(qū)域文字 plt.ylabel("均價") # 均價文字 plt.title(title) # 表標題文字 # 為每一個圖形加數(shù)值標簽 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 = '各區(qū)均價分析' average_price_bar(x, y, title)
運行如圖
全市二手房裝修程度分析
# 獲取各區(qū)二手房均價分析,根據(jù)需求,,進一步處理數(shù)據(jù),如果要寫相應算法,需要根據(jù)算法所需求的數(shù)據(jù)處理 def get_decorate_sum(): group = data.groupby('裝修') # 將房子區(qū)域分組 # decorate_sum_group = group['裝修'].count() # 計算每個區(qū)域的均價,average_price_group字典 decorate_sum_group = group.size() # 計算每個區(qū)域的均價,average_price_group字典 x = decorate_sum_group.index # 區(qū)域 y = decorate_sum_group.values.astype(int) # 區(qū)域對應的均價a =['t':'123'] a.keys() return x, y # 返回區(qū)域與對應的均價,region二關 average_price均價 # 顯示均價條形圖 def average_price_bar(x, y, title): plt.figure() # 圖形畫布 plt.bar(x, y, alpha=0.8) # 繪制條形圖 plt.xlabel("裝修類型") # 區(qū)域文字 plt.ylabel("數(shù)量") # 均價文字 plt.title(title) # 表標題文字 # 為每一個圖形加數(shù)值標簽 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)
各區(qū)二手房數(shù)量所占比比例
# 獲取各區(qū)二手房各區(qū)比例數(shù)量,進一步處理數(shù)據(jù),如果要寫相應算法,需要根據(jù)算法所需求的數(shù)據(jù)處理 def get_proportional_quantity(): area = data['區(qū)域'].groupby(data['區(qū)域']).count() # 將房子區(qū)域分組比例數(shù)量 areaName = (area).index.values # 將房子區(qū)域分組比例取名 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 = '各區(qū)二手房數(shù)量所占比比例' proportional_quantity_pie(area, areaName, title)
熱門戶型均價分析
# 獲取各區(qū)熱門戶型分析,根據(jù)需求,,進一步處理數(shù)據(jù),如果要寫相應算法,需要根據(jù)算法所需求的數(shù)據(jù)處理 def get_hot_portal(): # 另外一種方法獲取并取值 """ group = data.groupby('戶型').size # 將房子區(qū)域分組 sort_data = group.sort_values(ascending=False) # 將戶型分組數(shù)量進行降序 five_data = sort_data.head() # 提取前5組戶型數(shù)據(jù) 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('戶型') # 將房子區(qū)域分組 a = group['戶型'].count().sort_values(ascending=False).head() # 計算每個戶型的均價 字典 b = group['單價'].mean()[a.index] # 區(qū)域對應的均價a =['t':'123'] a.keys() x = b.index y = b.values.astype(int) return x, y # 返回區(qū)域與對應的均價,region二關 average_price均價 # 顯示均價橫條形圖 def hot_portal_barh(x, y, title): plt.figure() # 圖形畫布 plt.barh(x, y, alpha=0.9, color='red') # 繪制條形圖 plt.xlabel("均價") # 區(qū)域文字 plt.ylabel("戶型") # 均價文字 plt.title(title) # 表標題文字 plt.xlim(0, 15000) # X軸的大小 # 為每一個圖形加數(shù)值標簽 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)
前面三個圖較簡單,最后相對于前面三個較為麻煩
先獲取得到熱門戶型前五名,通過戶型得到對應的戶型的平均值
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!
相關文章
selenium學習教程之定位以及切換frame(iframe)
這篇文章主要給大家介紹了關于selenium學習教程之定位以及切換frame(iframe)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01pytorch中的scatter_add_函數(shù)的使用解讀
這篇文章主要介紹了pytorch中的scatter_add_函數(shù)的使用解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06