-
Book Overview & Buying
-
Table Of Contents
IPython Interactive Computing and Visualization Cookbook
By :
In this recipe, we will give an introduction to IPython for data analysis. Most of the subject has been covered in the Learning IPython for Interactive Computing and Data Visualization book, but we will review the basics here.
We will download and analyze a dataset about attendance on Montreal's bicycle tracks. This example is largely inspired by a presentation from Julia Evans (available at http://nbviewer.ipython.org/github/jvns/talks/blob/master/mtlpy35/pistes-cyclables.ipynb). Specifically, we will introduce the following:
In [1]: import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inlineurl that contains the address to a CSV (Comma-separated values) data file. This standard text-based file format is used to store tabular data:In [2]: url = "http://donnees.ville.montreal.qc.ca/storage/f/2014-01-20T20%3A48%3A50.296Z/2013.csv"
read_csv() function that can read any CSV file. Here, we pass the URL to the file. pandas will automatically download and parse the file, and return a DataFrame object. We need to specify a few options to make sure that the dates are parsed correctly:In [3]: df = pd.read_csv(url, index_col='Date',
parse_dates=True, dayfirst=True)df variable contains a DataFrame object, a specific pandas data structure that contains 2D tabular data. The head(n) method displays the first n rows of this table. In the notebook, pandas displays a DataFrame object in an HTML table, as shown in the following screenshot:In [4]: df.head(2)

First rows of the DataFrame
Here, every row contains the number of bicycles on every track of the city, for every day of the year.
describe() method:In [5]: df.describe()

Summary statistics of the DataFrame
Berri1 and PierDup. Then, we call the plot() method:In [6]: df[['Berri1', 'PierDup']].plot()

index attribute of the DataFrame object contains the dates of all rows in the table. This index has a few date-related attributes, including weekday:In [7]: df.index.weekday Out[7]: array([1, 2, 3, 4, 5, 6, 0, 1, 2, ..., 0, 1, 2])
However, we would like to have names (Monday, Tuesday, and so on) instead of numbers between 0 and 6. This can be done easily. First, we create a days array with all the weekday names. Then, we index it by df.index.weekday. This operation replaces every integer in the index by the corresponding name in days. The first element, Monday, has the index 0, so every 0 in df.index.weekday is replaced by Monday and so on. We assign this new index to a new column, Weekday, in DataFrame:
In [8]: days = np.array(['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday', 'Saturday',
'Sunday'])
df['Weekday'] = days[df.index.weekday]groupby() method lets us do just that. Once grouped, we can sum all the rows in every group:In [9]: df_week = df.groupby('Weekday').sum()
In [10]: df_week
Grouped data with pandas
ix (indexing operation). Then, we plot the table, specifying the line width:In [11]: df_week.ix[days].plot(lw=3)
plt.ylim(0); # Set the bottom axis to 0.
@interact decorator above our plotting function:In [12]: from IPython.html.widgets import interact
@interact
def plot(n=(1, 30)):
pd.rolling_mean(df['Berri1'], n).dropna().plot()
plt.ylim(0, 8000)
plt.show()
Interactive widget in the notebook
pandas is the right tool to load and manipulate a dataset. Other tools and methods are generally required for more advanced analyses (signal processing, statistics, and mathematical modeling). We will cover these steps in the second part of this book, starting with Chapter 7, Statistical Data Analysis.
Here are some more references about data manipulation with pandas:
Change the font size
Change margin width
Change background colour