0

I have a dataset of video games which has sales across 4 different regions JAPAN_Sales,NA_Sales,EUR_Sales,IND_Sales and Global_Sales. Sales are from the Year 1980 to 2016. I want to visualize the sum of Global_Sales from the Year 2000 to 2010. I am not able to do. I am a beginner in Pandas. I have tried the below code but stuck after that:

vg.groupby('Year').sum()

The column names are :

'Rank',
 'Name',
 'Platform',
 'Year',
 'Genre',
 'Publisher',
 'NA_Sales',
 'EUR_Sales',
 'JAP_Sales',
 'IND_Sales',
 'Global_Sales']
Python Learner
  • 147
  • 2
  • 12

1 Answers1

0

Your technique correctly sums per year, but it also attempts to sum genres and publishers etc.

  • We can limit the groupby to a single series that interests you, Global_Sales
  • Once that’s done, we get a series indexed on years, so we can use .loc[] with the years you want
  • sum again to get the total:
vg.groupby('Year')['Global_Sales'].sum().loc[2000:2010].sum()

Alternately get the sum of values directly

vg.loc[vg['Year'].between(2000, 2010), 'Global_Sales'].sum()
Cimbali
  • 11,012
  • 1
  • 39
  • 68
  • i am not getting the total sales from 2000 to 2010. I am only getting sales from 2000 to 2010 i.e. each year total sales. What i want is a single number which shows the total sales. – Python Learner Jul 07 '21 at 17:23
  • @PythonLearner simply add a `.sum()` − I’ve edited the answer. – Cimbali Jul 08 '21 at 09:16
  • Why sum() is there 2 times. Can i simply put the sum() method at the end once?.Please help me understand the logic behind it. – Python Learner Jul 08 '21 at 16:08
  • Once you’re getting the totals (i.e. sums) per year, then restricting the number of years, the getting the sum of the selected years. You can probably use a single sum too. – Cimbali Jul 08 '21 at 20:38
  • Using a single sum() is giving an error, i tried it. – Python Learner Jul 09 '21 at 09:44
  • You didn’t share your data @PythonLearner so I can only keep guessing and never test what I’m proposing. Also please add your error into the question [with the data](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – Cimbali Jul 09 '21 at 10:20
  • @Cimbsali. If you can guide me as to how to upload the dataset on stackoverflow that would be great. I am not able to upload the dataset on StackOverflow – Python Learner Jul 11 '21 at 06:41
  • @PythonLearner just put the output of `print(df.head())` or `print(df.sample(10))` in a code block − we definitely don’t need the whole dataset. – Cimbali Jul 11 '21 at 06:55
  • By the way @PythonLearner I tried with this second answer with the dataframe from your other question and it works – Cimbali Jul 11 '21 at 07:17