帕累托分析(貢獻度分析):即二八定律
目的:通過二八原則尋找屬于20%的關鍵決定性因素。
?
隨機生成數據
df = pd.DataFrame(np.random.randn(10)*1000+3000,index = list('ABCDEFGHIJ'),columns = ['銷量']) #避免出現負數
?
df.sort_values('銷量',ascending = False,inplace = True) #按大小倒序排序 df.plot(kind = 'bar',alpha = 0.7)s = df['銷量'].cumsum()/df['銷量'].sum() key = df[s >= 0.8].index[0] #累計銷量占比達到0.8的第一個索引標簽 position = df.index.tolist().index(key) #上述得到的索引標簽的位置 print('累計占比超過80%%的節點為%s,對應索引位置為%s'%(key,p)) print('關鍵產品為',df.index.tolist()[:position+1])s.plot(secondary_y = True,linestyle='--',marker='.',color = 'g') #在柱狀圖中使用y軸為副坐標軸,生成累計銷量占比的折線圖 plt.axhline(0.8,linestyle='--',color = 'r')
?