import os
import sys
os.listdir('.')
['us_population.csv', 'Example.ipynb', 'school_shootings.csv']
import gale.fileIO.csvHandlers as csvh
data, headers = csvh.read_as_list('school_shootings.csv', header=True)
print data[0]
['1850', '5']
import pandas as pd
df = pd.read_csv('school_shootings.csv')
print df.head()
Decade Count 0 1850 5 1 1860 6 2 1870 12 3 1880 16 4 1890 11
df.plot(x='Decade', y='Count')
<matplotlib.axes.AxesSubplot at 0x9ecfc6c>
Gross! Let's make that look nicer easily
from mpltools import style
style.use('ggplot')
df.plot(x='Decade', y='Count')
plt.ylabel('Number of School Shooting Incidents')
os.listdir('.')
uspop = pd.read_csv('us_population.csv')
uspop.head()
def pull_year(date_string):
return int(date_string.split(' ')[-1])
uspop['year'] = uspop.Date.apply(pull_year)
uspop.head()
uspop.plot(x='year', y='National Population')
plt.ylabel('US Population')
plt.xlabel('Year')
iPython already recognizes some of the basic bash commands and other shell commands can be run with a '!' before it.
ls
Example.ipynb school_shootings.csv us_population.csv
cd ~/Projects/Fragmented-Care/
/home/visitors/adampah/Projects/Fragmented-Care
ls -la
total 40 drwxrwxr-x 6 adampah adampah 4096 May 20 11:38 ./ drwxr-xr-x 27 adampah adampah 4096 May 29 11:21 ../ drwxr-xr-x 5 adampah adampah 4096 May 28 08:24 Code/ drwxr-xr-x 16 adampah adampah 4096 Jun 9 21:23 Data/ -rw-r--r-- 1 adampah adampah 4096 May 19 15:36 ._.DS_Store -rw-r--r-- 1 adampah adampah 6148 May 20 08:53 .DS_Store drwxr-xr-x 4 adampah adampah 4096 May 29 12:00 .hg/ -rw-r--r-- 1 adampah adampah 31 May 19 15:52 .hgignore drwxrwxr-x 3 adampah adampah 4096 Jun 9 16:34 Results/
!ping www.google.com
PING www.google.com (74.125.225.116) 56(84) bytes of data. 64 bytes from ord08s08-in-f20.1e100.net (74.125.225.116): icmp_req=1 ttl=56 time=1.61 ms 64 bytes from ord08s08-in-f20.1e100.net (74.125.225.116): icmp_req=2 ttl=56 time=1.57 ms 64 bytes from ord08s08-in-f20.1e100.net (74.125.225.116): icmp_req=3 ttl=56 time=1.57 ms 64 bytes from ord08s08-in-f20.1e100.net (74.125.225.116): icmp_req=4 ttl=56 time=1.62 ms 64 bytes from ord08s08-in-f20.1e100.net (74.125.225.116): icmp_req=5 ttl=56 time=1.63 ms ^C --- www.google.com ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4003ms rtt min/avg/max/mdev = 1.571/1.604/1.635/0.044 ms
But sometimes you may want to actually just execute a big block of bash code and we can do that too!
%%bash
cd $gale
ls -la
total 16 drwxrwxr-x 4 adampah adampah 4096 Jun 6 15:42 . drwxr-xr-x 23 adampah adampah 4096 Jun 2 18:52 .. drwxrwxr-x 14 adampah adampah 4096 Jun 3 19:43 gale drwxrwxr-x 4 adampah adampah 4096 Jun 6 15:42 .hg
That '%%' symbol is the invocation of a magic command (specifically a cell magic, you can do a line magic with just a single '%').
iPython notebook can run code from a number of other programming languages that way.
One thing to note though, is executing bash commands in a cell magic doesn't change the state of the actual notebook.
ls -la
total 40 drwxrwxr-x 6 adampah adampah 4096 May 20 11:38 ./ drwxr-xr-x 27 adampah adampah 4096 May 29 11:21 ../ drwxr-xr-x 5 adampah adampah 4096 May 28 08:24 Code/ drwxr-xr-x 16 adampah adampah 4096 Jun 9 21:23 Data/ -rw-r--r-- 1 adampah adampah 4096 May 19 15:36 ._.DS_Store -rw-r--r-- 1 adampah adampah 6148 May 20 08:53 .DS_Store drwxr-xr-x 4 adampah adampah 4096 May 29 12:00 .hg/ -rw-r--r-- 1 adampah adampah 31 May 19 15:52 .hgignore drwxrwxr-x 3 adampah adampah 4096 Jun 9 16:34 Results/
Even better we can actually transfer the results from a bash command to python!
files = !ls
for individual_file in files:
print individual_file
Code Data Results
Even better iPython can handle this with more languages than just bash too! At the current time it works with:
Ruby
Javascript
HTML
LaTeX
Perl
R
Julia
and more are always coming. Running them is also simple
%%ruby
puts "Oh snap. This is Ruby Code!"
Oh snap. This is Ruby Code!
%%javascript
console.log("Double damn! It's javascript!")
Magics aren't just used for running other languages, there are also other built-in, useful magics
%timeit map(float, [1, 2, 3, 4, 5] * 1000)
1000 loops, best of 3: 594 µs per loop
%timeit [float(x) for x in [1, 2, 3, 4, 5] * 1000]