I keep getting this error:
Exception: Data must be 1-dimensional
I am testing this code.
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()
My product_count.index
looks like this:
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')
My prod_count.values
looks like this:
array([[48],
[34],
[25],
[23],
[18],
[17],
[17],
[17],
[13],
[12],
[11],
[11],
[11],
[10],
[ 9],
[ 9],
[ 9],
[ 9],
[ 8],
[ 8]], dtype=int64)
I'm not sure why the field name says 'order_id' when I am doing an order count, but the dataframe should be like this.
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
And, the chart should look like this.
FYI, I found the plotting code on this site.
https://www.kaggle.com/tejainece/seaborn-barplot-and-pandas-value-counts
The problem is that your are trying to plot a pd.DataFrame (prod_count is a dataframe) which is not 1-dimensional. So you want to access the values from 'order_id' column from that dataframe. So try this instead : sns.barplot(prod_count.index, prod_count['order_id'].values, alpha=0.8)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.