我寫了一個條件語句,將房價轉換為范圍。但是,當我繪制顯示房屋價格的堆積條形圖時,我得到了一個專門用于價格為 0 美元的房屋的條形圖。我希望這些房子屬于 0-20 萬美元的類別conditions = [ (housing_data['sale_price'] < 200000) | (housing_data['sale_price']==0), (housing_data['sale_price'] > 200000) & (housing_data['sale_price'] < 400000), (housing_data['sale_price'] > 400000) & (housing_data['sale_price'] < 600000), (housing_data['sale_price'] > 600000) & (housing_data['sale_price'] < 800000), (housing_data['sale_price'] > 800000) & (housing_data['sale_price'] < 1000000), (housing_data['sale_price'] > 1000000) & (housing_data['sale_price'] < 10000000), (housing_data['sale_price'] > 10000000) & (housing_data['sale_price'] < 100000000),]choices =['$0-$200k','$200k-$400k','$400k-$600k','$600k-$800k','$800k-$1mlln','$1mlln-$10mlln','$10mlln-$100mlln']housing_data['price_range'] = np.select(conditions,choices)ax = housing_data.groupby(['year_of_sale','price_range']).size().unstack().plot.bar(stacked=True)horiz_offset = 1vert_offset = 1ax.legend(bbox_to_anchor=(horiz_offset, vert_offset))
1 回答

明月笑刀無情
TA貢獻1828條經驗 獲得超4個贊
檢查 cut
bins=[-100000000,20000,40000,60000,80000,100000,1000000,10000000]
housing_data['price_range']=pd.cut(housing_data['sale_price'],bins=bins,labels=choices )
添加回答
舉報
0/150
提交
取消