我不断收到此错误:
Exception: Data must be 1-dimensional
我正在测试这段代码。
prod_count = pd.DataFrame(df.groupby(['product_name'])['order_id'].count().sort_values(ascending=False).head(20))
plt.figure()
sns.barplot(prod_count.index, prod_count.values, alpha=0.8)
plt.title('Counts of Top Products Sold')
plt.ylabel('Number of Products', fontsize=12)
plt.xlabel('Products', fontsize=12)
plt.show()
我的product_count.index样子是这样的:
Index(['Banana', 'Bag of Organic Bananas', 'Organic Strawberries',
'Organic Hass Avocado', 'Limes', 'Strawberries', 'Organic Baby Spinach',
'Large Lemon', 'Organic Raspberries', 'Organic Garlic',
'Organic Avocado', 'Organic Yellow Onion', 'Organic Zucchini',
'Organic Gala Apples', 'Cucumber Kirby', 'Organic Red Onion',
'Organic Whole Milk', '100% Whole Wheat Bread', 'Organic Cilantro',
'Apple Honeycrisp Organic'],
dtype='object', name='product_name')
我的prod_count.values样子是这样的:
array([[48],
[34],
[25],
[23],
[18],
[17],
[17],
[17],
[13],
[12],
[11],
[11],
[11],
[10],
[ 9],
[ 9],
[ 9],
[ 9],
[ 8],
[ 8]], dtype=int64)
我不确定为什么在进行订单计数时字段名称会显示“order_id”,但数据框应该是这样的。
product_name order_id
Banana 48
Bag of Organic Bananas 34
Organic Strawberries 25
Organic Hass Avocado 23
Limes 18
Strawberries 17
Organic Baby Spinach 17
Large Lemon 17
Organic Raspberries 13
Organic Garlic 12
Organic Avocado 11
Organic Yellow Onion 11
Organic Zucchini 11
Organic Gala Apples 10
Cucumber Kirby 9
Organic Red Onion 9
Organic Whole Milk 9
100% Whole Wheat Bread 9
Organic Cilantro 8
Apple Honeycrisp Organic 8
而且,图表应该是这样的。
仅供参考,我在这个网站上找到了绘图代码。
https://www.kaggle.com/tejainece/seaborn-barplot-and-pandas-value-counts
