……
書接上文
4.車輛等級維度
探查車齡為5年的車輛,折舊價值與車輛等級的關(guān)系。
# 篩選出車齡為5的數(shù)據(jù)創(chuàng)建新表
data_age5 = data[data['age'] == 5]
data_age5
# 分組聚合計算均值
data_car_level = data_age5.groupby('car_level_name')['lowest_price'].mean().reset_index()
data_car_level
這里用到了 DataFrame 的 groupby 函數(shù),這個函數(shù)對于數(shù)據(jù)處理的重要程度無需贅言。
groupby 必須配合聚合函數(shù)同時使用,否則只能得到一個 DataFrameGroupBy 類型的玩意兒。
這里是可以只傳 groupby 參數(shù),不寫聚合函數(shù)作用的字段的,也就是:
data_age5.groupby('car_level_name').mean()
這樣的效果和前面提到的 describe 函數(shù)相似,會對所有數(shù)值字段進行聚合計算。
這里還用到了 reset_index 函數(shù),可以給生成的新表添加一列數(shù)字索引。
data_car_level_sort = data_car_level.sort_values('lowest_price',ascending = False)
data_car_level_sort
創(chuàng)建新表,使用 sort_values 函數(shù)對數(shù)據(jù)進行排序。第一個參數(shù)必穿,是排序的數(shù)值列名,第二個參數(shù)是調(diào)整升序降序,默認升序,給參數(shù) False 可以改為降序。
5.標簽維度——可視化
根據(jù)標簽,對比5年車齡車輛殘值價格。
嘗試使用另一種分組聚合方式——數(shù)據(jù)透視表:
data_picture = data_age5.pivot_table(index = 'maker_type', values = ['lowest_price'],aggfunc=np.mean)
bar_data_picture = data_picture.reset_index()
bar_data_picture
使用 pivot_table 函數(shù),含義與 group by 相同,索引 index 就是分組的列,值 values 就是需要聚合計算的數(shù)值列,聚合函數(shù) aggfunc 使用 numpy 包中的聚合函數(shù),這里依舊取平均值。
# 設(shè)置字體
plt.rcParams['font.sans-serif'] = ['SimHei']
# 設(shè)置編碼,保證圖表中中文和符號正常顯示
plt.rcParams['axes.unicode_minus'] = False
# 定義x軸和y軸都是哪些數(shù)據(jù)
sns.barplot(x = bar_data_picture['maker_type'], y = bar_data_picture['lowest_price'], ci=68)
# 設(shè)置標題
plt.title('五年車齡二手車價格對比')
plt.show()
使用 matplotlib.pyplot 包進行可視化屬性參數(shù)配置。
更多配置詳情請看:
http://t.csdn.cn/Kkx8Ihttp://t.csdn.cn/Kkx8I使用 seaborn 包傳輸圖像必須的橫縱坐標數(shù)據(jù)并展示。
注:查看matplotlib默認配置參數(shù):print(plt.rcParams)
6.品牌維度——箱線圖
data_brand = data_age5.groupby('brand')['lowest_price'].mean().reset_index()
data_brand_sort_desc = data_brand.nlargest(5, 'lowest_price')
data_brand_sort_desc
同樣分組聚合,查看各個品牌的車輛殘值價格平均數(shù),然后取價格最高的前五個品牌。
使用 nlargest 函數(shù)可以輕松獲得,與之相對的還有取最小值的前n個元素的函數(shù)?nsmallest 。
data_brand_sort_desc_plot = data_age5[data_age5['brand'].isin(data_brand_sort_desc.brand)]
data_brand_sort_desc_plot
反向取得價格平均數(shù)前五的品牌的車輛全部信息。
這里的?data_brand_sort_desc.brand 和?data_brand_sort_desc['brand'] 含義相同,也是提取表的一列。
isin 函數(shù)使用方式和SQL類似,利用布爾索引判斷目標表的某一列值是否在條件列中。
上述過程相當于SQL的:
select
t1.*
from 總表 t1
where lowest_price in (
select
lowest_price
,brand
from (
select
avg(lowest_price) lowest_price
,brand
from 總表
group by brand
) t2
order by lowest_price desc
limit 5
)
對比SQL的復(fù)雜寫法和嵌套邏輯,python代碼看起來就簡單多了(看起來)。
sns.boxplot(x='brand',y='lowest_price',data=data_brand_sort_desc_plot)
然后進行可視化,使用 boxplot 函數(shù)生成箱線圖,傳入 x 軸?y 軸以及表名,可以觀察到數(shù)據(jù)的中位數(shù)、上下四分位數(shù)、異常值分布。
關(guān)于箱線圖怎么看,以及 boxplot 函數(shù)的詳細使用,和參數(shù)修改,可以參考:http://t.csdn.cn/jNh8Xhttp://t.csdn.cn/jNh8X
7.相關(guān)性分析——熱力圖
python有一個探查不同維度之間相關(guān)性的常用函數(shù),corr:
correlation = data.corr()
correlation
這是在對全體數(shù)據(jù)進行各維度相關(guān)性探查,corr 函數(shù)會將 DataFrame 表中所有數(shù)值類型的列進行相關(guān)性計算,計算結(jié)果在 -1 和 1 之間,結(jié)果越接近 1 表示兩個維度值之間越正相關(guān),越接近 -1 則說明兩個維度值之間越呈負相關(guān)。
sns.heatmap(correlation, linewidth = 1.0, linecolor = 'white', square = True, annot = True, vmax=1.0)
# annot是否顯示值
# vmax熱力圖取值顏色最大值
# square是否是正方形
緊接著用 heatmat 函數(shù)生成多熱力圖查看數(shù)據(jù)相關(guān)度整體情況,通過調(diào)整配置參數(shù)值,可以輕松看到數(shù)據(jù)各個維度的相關(guān)性。
結(jié)論:
二手車價格與新車價格呈很強的正相關(guān);
車齡與二手車殘值率呈很強的負相關(guān);
車齡與二手車凈殘值也呈負相關(guān),但沒有殘值率體現(xiàn)的明顯。文章來源:http://www.zghlxwxcb.cn/news/detail-472252.html
(完)文章來源地址http://www.zghlxwxcb.cn/news/detail-472252.html
到了這里,關(guān)于python筆記17_實例演練_二手車折舊分析p2的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!