"""
## Video Game Sales Exploratory Data Analysis by Ammar Altalibi

Uses 'Sales Of Video Games' dataset from Kaggle

Link to dataset: 
https://www.kaggle.com/arslanali4343/sales-of-video-games

The data is comprised of video games from 1980-2016 and their sales across NA, EU, JP, and total Global sales.


---



This is my exploratory data analysis of global video games sales. The notebook is divided into sections, with each section divided into subsections based on what questions I have. For the Data Analysis section, I included my thoughts and observations under each graph as well.
"""
import pandas as pd
import numpy as np
import seaborn as sns
%matplotlib inline
import matplotlib.pyplot as plt
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))
# Read the csv file into a dataframe
data = pd.read_csv('/kaggle/input/sales-of-video-games/vgsales.csv', sep = ',', encoding = 'Latin-1')

# Display the first 5 rows
data.head()
"""
## Data Cleaning
"""
"""
Check to see the data types and if there are any null values
"""
data.info()
"""
We can see that we have some missing values in columns 'Year', 'Publisher', and 'Global_Sales'. We also need to convert Year from a string into an integer format.
"""
"""
Count the number of rows with null values, and then drop the rows that are missing global sales values
"""
data.isnull().sum()
data = data.dropna(subset = ['Global_Sales'])
"""
Check the shape of our dataset (number of rows and columns)
"""
print('There are',data.shape[0], 'rows in our dataframe')
print('There are',data['Name'].nunique(), 'unique video games in our dataframe')
print('There are',data.shape[1], 'columns in our dataframe')
"""
Converting the Year column to datetime format
"""
data['Year'] = pd.to_datetime(data['Year'], format = '%Y')
"""
## Data Exploration
"""
"""
Count the number of unique video games for each publisher and display the top 5
"""
data.groupby('Publisher')[['Name']].nunique().sort_values(by = 'Name', ascending = False).head()
"""
Count the number of unique video games for each platform and display the top 5
"""
data.groupby('Platform')[['Name']].count().sort_values(by = 'Name', ascending = False).head()
"""
Here are the top 10 highest selling video games globally
"""
data[['Name', 'Platform', 'Year','Publisher','Global_Sales']].head(10).sort_values(by = 'Global_Sales', ascending = False)
"""
## Data Analysis

After getting familiar with our data, we can begin analyzing the data further to find any trends, patterns, or any useful insights such as relationships. This section will divided based on the main question asked.
"""
"""
We first groupby publisher, and then sort the values by sales and taking the top 20, before creating a barplot
"""
lmdata = data.groupby('Publisher')[['Publisher','Global_Sales']].sum().sort_values('Global_Sales', ascending = False).head(20)

lmdata['Global_Sales'].plot(kind = 'bar', 
                            figsize = (16,6), 
                            colormap = 'Set3', 
                            ylabel = 'Global Sales (Millions)', 
                            title = 'Global Video Games Sales by Publisher');
"""
Observations:

We can see that Nintendo has the greatest global sales out of any publisher, with nearly 1.8 billion in total sales, and is followed by Electronic Arts, at 1.1 billion. Lets look into Nintendo a little further.
"""
"""
### Analyzing Nintendo

Let's create a lineplot that shows us the total global sales for Nintendo each year
"""
ndata = data[data['Publisher'] == 'Nintendo']
ndata = ndata.groupby('Year')[['Year','NA_Sales','EU_Sales','JP_Sales','Global_Sales']].sum()

fig = plt.figure(figsize = (14,8))
sns.lineplot(data = ndata, x = 'Year', y = 'Global_Sales');
"""
Observations:

For the first 20 years, Nintendo sales seem to follow a relatively consistent pattern that spikes every 3-4 years. However, there is a very steep incline from 2004-2007. What caused this?
"""
ndata2 = data[data['Publisher'] == 'Nintendo']
ndata2 = ndata2[(ndata2['Year'] >= '2004') & (ndata2['Year'] <= '2007')]
ndata2 = ndata2[['Name','Platform','Year','Global_Sales']]
ndata2.head()
"""
Observations:

We can see that 'Wii Sports', the highest selling Nintendo game of all time, was released during this timeframe, which would explain the dramatic increase in sales
"""
"""
### Analyzing Electronic Arts

We'll make a similar lineplot to the one made for Nintendo, in order to see the sales for EA
"""
eadata = data[data['Publisher'] == 'Electronic Arts']
eadata = eadata.groupby('Year')[['Year','NA_Sales','EU_Sales','JP_Sales','Global_Sales']].sum()

fig = plt.figure(figsize = (14,8))
sns.lineplot(data = eadata, x = 'Year', y = 'Global_Sales');
"""
Observations:

Unlike Nintendo, EA had a slower but more stable increase in sales. There seems to be a few years where sales sharply declined, such as 1998 and 2005, but sales recovered quickly afterwards.
"""
"""
## Conclusions

While the data was a bit messy originally, simple data cleaning and reformating solved any issues I ran across. I also found it very interesting just how many different games were released for the Nintendo DS console (over 2100 games!) with some being released around 2014, despite the console originally releasing in 2004. 

Nintendo outselling its competitors globally was no surprise, but it was interesting seeing just out much it's able to outsell the others was fascinating. As a fan of Nintendo games since I was a child, I can't wait to see just how far Nintendo can progress in the upcoming years. Thanks for reading!
"""