Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Math 480: Open Source Mathematical Software
2016-05-11
William Stein
Lectures 20: Pandas (part 2 of 3)
Today:
Reminder: your homework and peer grading is due Friday at 6pm. Update about peer grading guidelines.
Start the screencast.
More pandas...
getting data into SMC
Series
DataFrames
1. Getting data into SMC
Some deer at the Google data center
in Iowa where SMC lives.
WARNING: SMC has absolutely no direct access to the files on your laptop. It is a remote computer account running in the cloud. That said, if you use pandas/python, etc, on your own computer at some point, it will of course have direct access. (Maybe do a live demo of
python get-pip.py
thenpip install --user pandas
on my mac laptop.)+New, paste the link into the box, click "Download from Internet".
+New, drag and drop, upload a file (only up to 30MB right now).
Use
wget
orcurl
from the terminal or in a %sh cell.Use
scp
orrsync
(see project settings... SSH button) from your computer to the project.
I've never mentioned %sh
before -- it's just like the terminal but from a worksheet. As long as a command isn't interactive, it should work for you...
pd.Series
http://pandas.pydata.org/pandas-docs/version/0.18.1/dsintro.html#series
Datasets are built out of
pd.Series
objects, which are like a List of values, but with a specific type (like floating point number, integer, string, ...) and an index. More precisely, apd.Series
is a "One-dimensional ndarray with axis labels"Larger
pd.DataFrame
objects are built out of several such series. Each of them is a column, must have the same length, and each column has a name.
Examples:
Indexing...
... so a pd.Series is much like a Python dict except you can do arithmetic, there is an order, you can have the same key multiple times, indexing is amazing. And everything is super fast.
Exercise right now!:
Make a Python dict
d
of your choosing, then try doingpd.Series(d)
. It should work.Make up 3 different pd.Series objects with various choices of values and index.
Try adding them together and see what happens.
I got this in a "spam" email from Enthought today on a course they run about Pandas!
"50 to 80 percent of time is spent wading through the tedium of the first two steps – acquiring and wrangling data – before even getting to the real work of analysis and insight." -- New York Times
pd.DataFrame
Documentation: http://pandas.pydata.org/pandas-docs/version/0.18.1/dsintro.html#dataframe
DataFrame = "a 2-dimensional labeled data structure with columns of potentially different types."
Think of it as:
a spreadsheet, or
a SQL table, or
a dict of
pd.Series
objects.
Now, we build a pd.DataFrame
:
age | name | |
---|---|---|
0 | 42 | William |
1 | 68 | Marlene |
2 | 19 | Jon |
3 | 22 | Jane |
name | age | |
---|---|---|
0 | William | 42 |
1 | Marlene | 68 |
2 | Jon | 19 |
3 | Jane | 22 |
The bold numbers or values on the left are called the "index".
The index is used to uniquely identify a row.
Use people.ix[i] to get the row with given index!
This isn't obvious. You just have to memorize it. Learn it now.
Of course, the rows don't have to be indexed by numbers -- anything hashable is allowed.
col1 | col2 | |
---|---|---|
name | william | jon |
rank | private | general |
ser | NaN | 5077 |
** Exercise right now!** Make a data frame for which there are two rows with the same index i, showing that the index need not be unique! [Hint: we made such a series above]
What happens when you type d.ix[i]
?
NOTE: Besides .ix
, there are also other properties for various purposes. Read more about them here:
col1 | col2 | |
---|---|---|
name | william | jon |
rank | private | general |
ser | NaN | 5077 |
col1 | col2 | |
---|---|---|
name | william | jon |
rank | private | general |
Question: How to get all rows starting from index 1 up to the end?
Answer: slicing.
age | name | |
---|---|---|
0 | 42 | William |
1 | 68 | Marlene |
2 | 19 | Jon |
3 | 22 | Jane |
age | name | |
---|---|---|
0 | 42 | William |
1 | 68 | Marlene |
age | name | |
---|---|---|
1 | 68 | Marlene |
2 | 19 | Jon |
age | name | |
---|---|---|
0 | 42 | William |
2 | 19 | Jon |
CSV
CSV is a very simple column-oriented data format, where a text file contains rows and a comma is a delimiter for the columns.
The first row contains the header, describing the columns.
Pandas then imports this file via pd.read_csv()
(the argument of this function could not only be a filename, but also a URL link 'http://...' pointint to a csv file)
name | type | tail | legs | age | color | |
---|---|---|---|---|---|---|
0 | joe | elephant | Y | 4 | 16.0 | gray |
1 | tim | dog | Y | 4 | 7.0 | golden |
2 | susan | cat | Y | 4 | 4.0 | black |
3 | frank | fish | N | 0 | 1.0 | green |
4 | olivia | spider | N | 6 | 0.5 | black |
5 | minki | cat | Y | 4 | 6.0 | white |
The power of Pandas is, that you can start asking questions about this data, filter it, compare it with other datasets, or even picture it in a plot very easily.
legs | age | |
---|---|---|
count | 6.000000 | 6.000000 |
mean | 3.666667 | 5.750000 |
std | 1.966384 | 5.654644 |
min | 0.000000 | 0.500000 |
25% | 4.000000 | 1.750000 |
50% | 4.000000 | 5.000000 |
75% | 4.000000 | 6.750000 |
max | 6.000000 | 16.000000 |
name | type | tail | legs | age | color | |
---|---|---|---|---|---|---|
2 | susan | cat | Y | 4 | 4.0 | black |
3 | frank | fish | N | 0 | 1.0 | green |
4 | olivia | spider | N | 6 | 0.5 | black |
name | type | tail | legs | age | color | |
---|---|---|---|---|---|---|
0 | joe | elephant | Y | 4 | 16.0 | gray |
1 | tim | dog | Y | 4 | 7.0 | golden |
2 | susan | cat | Y | 4 | 4.0 | black |
5 | minki | cat | Y | 4 | 6.0 | white |
Question: which animal(s) has/have the largest number of legs?
Plotting
In a related library "statsmodels", there are also various datasets included. Try plotting one of them (where it makes sense)
violent | murder | hs_grad | poverty | single | white | urban | |
---|---|---|---|---|---|---|---|
state | |||||||
Alabama | 459.9 | 7.1 | 82.1 | 17.5 | 29.0 | 70.0 | 48.65 |
Alaska | 632.6 | 3.2 | 91.4 | 9.0 | 25.5 | 68.3 | 44.46 |
Arizona | 423.2 | 5.5 | 84.2 | 16.5 | 25.7 | 80.0 | 80.07 |
Arkansas | 530.3 | 6.3 | 82.4 | 18.8 | 26.3 | 78.4 | 39.54 |
California | 473.4 | 5.4 | 80.6 | 14.2 | 27.8 | 62.7 | 89.73 |
Colorado | 340.9 | 3.2 | 89.3 | 12.9 | 21.4 | 84.6 | 76.86 |
Connecticut | 300.5 | 3.0 | 88.6 | 9.4 | 25.0 | 79.1 | 84.83 |
Delaware | 645.1 | 4.6 | 87.4 | 10.8 | 27.6 | 71.9 | 68.71 |
District of Columbia | 1348.9 | 24.2 | 87.1 | 18.4 | 48.0 | 38.7 | 100.00 |
Florida | 612.6 | 5.5 | 85.3 | 14.9 | 26.6 | 76.9 | 87.44 |