Exploratory Data Analysis of the FIFA 19 Dataset in Python | by Sadrach Pierre, Ph.D. | Towards Data Science

In this post we will perform simple exploratory data analysis of the FIFA 19 data set. The data set can be found on Kaggle. FIFA is the Fédération Internationale de Football Association and FIFA 19 is part of the FIFA series of association football video games. It is one of the best selling video games of all time selling over 260 million copies to date.
For this analysis we will be using the python pandas library, numpy, seaborn and matplotlib. The dataset contains 89 columns but we will limit our analysis to the following ten columns:
Name — Name of PlayerAge — Age of PlayerNationality — Nationality of PlayerValue — Current Market ValueWage — Wage of PlayerPreferred Foot — Preferred foot of playerHeight — Height of PlayerWeight — Weight of playerPosition — Position on the pitchOverall — Player’s Overall Rating
Name — Name of Player
Age — Age of Player
Nationality — Nationality of Player
Value — Current Market Value
Wage — Wage of Player
Preferred Foot — Preferred foot of player
Height — Height of Player
Weight — Weight of player
Position — Position on the pitch
Overall — Player’s Overall Rating
Let’s get started!
First let’s import the pandas library and read the csv file into a pandas dataframe and print the first five rows for the first sixteen columns:
import pandas as pddf = pd.read_csv("data.csv")print(df.head())
We can filter the dataframe so it only includes the ten columns we want:
df = df[['Name', 'Age', 'Nationality', Value', 'Wage', 'Preferred Foot', 'Height', 'Weight', Position', 'Overall']]print(df.head())
First thing we can do is generate some statistics on the height column. The values are given as strings so we will want to convert them to a number we can use in calculations. Here we convert the height strings into centimeter values. We do this by multiplying the first element of the string, for example ‘5’7’, by 12.0 (inches) and we add the third element of the string ‘7’ (which is in inches) and multiply the final result by 2.54 (centimeters):
Height_cm = []for i in list(df['Height'].values):    try:        Height_cm.append((float(str(i)[0])*12.0 + float(str(i)[2:]))*2.54)    except(ValueError):        Height_cm.append(np.nan)        df['Height_cm'] = Height_cm
First we initialize a list called ‘Height_cm’ and we iterate over the height string value in the dataframe. We insert the conversion statements in between try except clause since there are cases where the strings contain non-numeric values, specifically there are missing height values. Whenever we reach a missing value we append an np.nan (numpy ‘not a number’) value. Finally we defined the new column to be the list we just populated.
Next let’s remove the missing values from the dataframe and print the first five rows:
df.dropna(inplace = True)print(df['Height_cm'].head())
We can look at the mean and standard deviation in Height in cm:
print("Mean: ", df['Height_cm'].mean())print("Standard Deviation: ", df['Height_cm'].std())
We can also generate a histogram of the heights:
import seaborn as sns sns.set()df['Height_cm'].hist(bins = 10)
We can also wrap this up in a function and resuse it for any numeric column:
import seaborn as sns     import matplotlib.pyplot as pltdef get_statistics(numeric_column_name):    print("Mean {}: ".format(numeric_column_name),   df[numeric_column_name].mean())    print("Standard Deviation in {}: ".format(numeric_column_name), df[numeric_column_name].std())    sns.set()    plt.title("{} Histogram".format(numeric_column_name))    df[numeric_column_name].hist(bins = 10)    get_statistics('Height_cm')
which generates the same output as above.
We can also convert the weight column to kilograms for consistent use of the metric system. We do this by taking the first three elements of each string values in the ‘Weight’ column, convert them into floating point values and the divide by the conversion factor between Kg and pounds (2.20462):
df['Weight_kg'] = df['Weight'].str[:3].astype(float)/2.20462print(df.head())
We can call our generate_statistics function with ‘Weight_kg’:
get_statistics('Weight_kg')
We can also look at a the frequency in Nationality of each player using the ‘Counter’ method from the collections module:
from collections import Counterprint(Counter(df['Nationality'].values))
We can limit the counter to output only the five most common nationalities:
print(Counter(df['Nationality'].values).most_common(5))
As you can see England is the most frequent nationality with 1,657 records, followed by Germany with 1,195 and Spain with 1,071. We can display this output in a bar chart:
bar_plot = dict(Counter(df['Nationality'].values).most_common(5))plt.bar(*zip(*bar_plot.items()))plt.show()
Again, we can define a function that let’s us plot the most common values for any categorical column:
def plot_most_common(category):    bar_plot = dict(Counter(df[category].values).most_common(5))    plt.bar(*zip(*bar_plot.items()))    plt.show()
If we call the function with ‘Position’ we get:
plot_most_common('Position')
Next let’s convert the value (which is the market value for the player) and the wage into numeric values we can use in calculations. First lets truncate the string values in the ‘Value’ column such that we remove the ‘Euro’ and the ‘M’. The we can then convert each string into a floating point and multiply by 1e6:
value_list = []for i in list(df['Value'].values):    try:        value_list.append(float(i)*1e6)    except(ValueError):        value_list.append(np.nan)df['Value_numeric'] = value_list
We can do the same for ‘Wage’, but here we multiply by 1e3:
wage_list = []for i in list(df['Wage'].values):    try:        wage_list.append(float(i)*1e3)    except(ValueError):        wage_list.append(np.nan)df['Wage_numeric'] = wage_list
We will also want to convert the string values in the ‘Age’ column into integers:
df['Age'] = df['Age'].astype(int)
From here we can generate a heat map from the numerical columns which will show us how strongly correlated each variable is with the other. Let’s filter the dataframe so that it only includes numerical values and generate a heat map from the resulting dataframe:
numerical_columns = df[['Height_cm', 'Weight_kg', 'Value_numeric', 'Age', 'Wage_numeric']]sns.heatmap(numerical_columns.corr(), annot=True)plt.show()
We can also use box plots to visualize the distribution in numeric values based on the minimum, maximum, median, first quartile, and third quartile. If you are unfamiliar with them take a look at the article Understanding Boxplots.
For example the distribution in market value for England, Germany and Spain:
df = df[df['Nationality'].isin(['England', 'Germany', 'Spain'])]sns.boxplot(x= df['Nationality'], y = df['Value_numeric'])plt.show()
And the distribution in age:
sns.boxplot(x= df['Nationality'], y = df['Age'])plt.show()
The distribution in weight:
sns.boxplot(x= df['Nationality'], y = df['Weight_kg'])plt.show()
And height:
sns.boxplot(x= df['Nationality'], y = df['Height_cm'])plt.show()
I’ll stop here but feel free to play around with the data and code yourself. The code from this post is available on GitHub. Thank you for reading!