0

I would like to take only product equal to "Bikes" from the table below and group the years. I need it to create a plot where i want show how the sale of bicycles has been over the years.

yearsbuy = df[['Year', 'Product_Category', 'Country']]
plot1 = yearsbuy.groupby('Year')['Product_Category'].value_counts()
plot1 = plot1['Product_Category'] =='Bikes'

When i did i get :

Year  Product_Category
2011  Bikes                2677
2012  Bikes                2677
2013  Accessories         15025
      Bikes                5710
      Clothing             3708
2014  Accessories         20035

But I want just only Bikes. Why yearsbuy['Product_Category'] == "Bikes" isn't working ?

Ralubrusto
  • 1,394
  • 2
  • 11
  • 24
Przemek Dabek
  • 519
  • 2
  • 14

1 Answers1

0

When we do a value_counts(), it gives the count as a pandas.Series for selected column, making the Year and Product_Category as indices (Mutli-index).

To obtain the columns Year and Product_Category back, we've to reset the index but the generated series raises a conflict since by default the Series takes the name of the column we're working with (Product_Category). So I'm renaming that column containing count as count and resetting index to obtain the Year and Product_Category columns

plot1 = yearsbuy.groupby('Year')['Product_Category'].value_counts().rename('count').reset_index()
plot1 = plot1[plot1['Product_Category'] =='Bikes']
Raghul Raj
  • 1,428
  • 9
  • 24