Home > database >  sum numbers with the same 'Year' from .csv file
sum numbers with the same 'Year' from .csv file

Time:12-11

Question: How can i sum 'Global_Sales' with the same 'Year' using "pandas". I really have no idea how to do this.

P.S Also I need to do "Plot", but I know how to do it.

My code:

import pandas as pd
import matplotlib.pyplot as plt

plt(plot)

Data from 'vgsales.csv':

Rank,Name,Platform,Year,Genre,Publisher,NA_Sales,EU_Sales,JP_Sales,Other_Sales,Global_Sales
1,Wii Sports,Wii,2006,Sports,Nintendo,41.49,29.02,3.77,8.46,82.74
2,Super Mario Bros.,NES,1985,Platform,Nintendo,29.08,3.58,6.81,0.77,40.24
...
24,Grand Theft Auto V,X360,2013,Action,Take-Two Interactive,9.63,5.31,0.06,1.38,16.38
...
45,Grand Theft Auto V,PS4,2014,Action,Take-Two Interactive,3.8,5.81,0.36,2.02,11.98
etc

CodePudding user response:

Here is a starting point:

import pandas as pd

df = pd.read_csv (r'C:\temp\sales.csv') # read the csv file

df.groupby(by='Year').sum()  # group by sales year and get the sum

As mentioned in the comments, getting the sum doesn't involve Matplotlib.

CodePudding user response:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('vgsales.csv')
df.head()

df_1 = df.groupby(by='Year').sum()
plt.plot(df_1['Global_Sales'])
  • Related