# Render our plots inline
%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
# Make the graphs a bit prettier, and bigger
plt.style.use('ggplot')
plt.rcParams['figure.figsize'] = (15, 5)
You can read data from a CSV file using the read_csv
function. By default, it assumes that the fields are comma-separated.
We're going to be looking some cyclist data from Montréal. Here's the original page (in French), but it's already included in this repository. We're using the data from 2012.
This dataset is a list of how many people were on 7 different bike paths in Montreal, each day.
data_url = "https://sciencedata.dk/public/6e3ed434c0fa43df906ce2b6d1ba9fc6/pandas-cookbook/data/bikes.csv"
broken_df = pd.read_csv(data_url,encoding = "ISO-8859-1")
# Look at the first 3 rows
broken_df[:3]
You'll notice that this is totally broken! read_csv
has a bunch of options that will let us fix that, though. Here we'll
change the column separator to a ;
Set the encoding to 'latin1'
(the default is 'utf8'
)
Parse the dates in the 'Date' column
Tell it that our dates have the day first instead of the month first
Set the index to be the 'Date' column
fixed_df = pd.read_csv(data_url, sep=';', encoding='latin1', parse_dates=['Date'], dayfirst=True, index_col='Date')
fixed_df[:3]
When you read a CSV, you get a kind of object called a DataFrame
, which is made up of rows and columns. You get columns out of a DataFrame the same way you get elements out of a dictionary.
Here's an example:
fixed_df['Berri 1']
Just add .plot()
to the end! How could it be easier? =)
We can see that, unsurprisingly, not many people are biking in January, February, and March,
fixed_df['Berri 1'].plot()
We can also plot all the columns just as easily. We'll make it a little bigger, too.
You can see that it's more squished together, but all the bike paths behave basically the same -- if it's a bad day for cyclists, it's a bad day everywhere.
fixed_df.plot(figsize=(15, 10))
Here's the code we needed to write do draw that graph, all together:
df = pd.read_csv(data_url, sep=';', encoding='latin1', parse_dates=['Date'], dayfirst=True, index_col='Date')
df['Berri 1'].plot()