我只想从下表中取出等于 "Bikes"
的乘积并将年份分组。我需要它来创建一个图来显示多年来自行车的销售情况。
yearsbuy = df[['Year', 'Product_Category', 'Country']]
plot1 = yearsbuy.groupby('Year')['Product_Category'].value_counts()
plot1 = plot1['Product_Category'] =='Bikes'
我什么时候得到的:
Year Product_Category
2011 Bikes 2677
2012 Bikes 2677
2013 Accessories 15025
Bikes 5710
Clothing 3708
2014 Accessories 20035
但我只想要自行车。为什么 yearsbuy['Product_Category'] == "Bikes"
不起作用?
答案 0 :(得分:0)
当我们执行 value_counts()
时,它会为所选列提供 Pandas.Series 的计数,使 Year
和 Product_Category
作为索引(Mutli-index)。
要返回 Year
和 Product_Category
列,我们必须重置索引,但生成的系列会引发冲突,因为默认情况下,系列采用我们正在处理的列的名称(Product_Category
)。
所以我将包含计数的列重命名为 count
并重置索引以获得 Year
和 Product_Category
列
plot1 = yearsbuy.groupby('Year')['Product_Category'].value_counts().rename('count').reset_index()
plot1 = plot1[plot1['Product_Category'] =='Bikes']