Data Visualization With Python
Exploring Datasets with pandas
pandas is an essential data analysis toolkit for Python. From their website:
pandas is a Python package providing fast, flexible, and expressive data structures designed to make working with “relational” or “labeled” data both easy and intuitive. It aims to be the fundamental high-level building block for doing practical, real world data analysis in Python.
The course heavily relies on pandas for data wrangling, analysis, and visualization. We encourage you to spend some time and familizare yourself with the pandas API Reference: http://pandas.pydata.org/pandas-docs/stable/api.html.
The Dataset: Immigration to Canada from 1980 to 2013
Dataset Source: International migration flows to and from selected countries - The 2015 revision.
The dataset contains annual data on the flows of international immigrants as recorded by the countries of destination. The data presents both inflows and outflows according to the place of birth, citizenship or place of previous / next residence both for foreigners and nationals. The current version presents data pertaining to 45 countries.
In this lab, we will focus on the Canadian immigration data.
For sake of simplicity, Canada’s immigration data has been extracted and uploaded to one of IBM servers. You can fetch the data from here.
pandas Basics
The first thing we’ll do is import two key data analysis modules: pandas and Numpy.
import numpy as np # useful for many scientific computing in Python
import pandas as pd # primary data structure library
Let’s download and import our primary Canadian Immigration dataset using pandas read_excel()
method. Normally, before we can do that, we would need to download a module which pandas requires to read in excel files. This module is xlrd. For your convenience, we have pre-installed this module, so you would not have to worry about that. Otherwise, you would need to run the following line of code to install the xlrd module:
!conda install -c anaconda xlrd --yes
Now we are ready to read in our data.
!conda install -c anaconda xlrd --yes
df_can = pd.read_excel('https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DV0101EN/labs/Data_Files/Canada.xlsx',
sheet_name='Canada by Citizenship',
skiprows=range(20),
skipfooter=2)
print ('Data read into a pandas dataframe!')
Let’s view the top 5 rows of the dataset using the head()
function.
df_can.head() # tip: You can specify the number of rows you'd like to see as follows: df_can.head(10)