Analyzing Data from Multiple Files


In [4]:
import glob

# The glob library contains a single function, also called glob, 
# that finds files whose names match a pattern.

In [5]:
print(glob.glob('data/inflammation*.csv'))


['data/inflammation-01.csv', 'data/inflammation-02.csv', 'data/inflammation-03.csv', 'data/inflammation-04.csv', 'data/inflammation-05.csv', 'data/inflammation-06.csv', 'data/inflammation-07.csv', 'data/inflammation-08.csv', 'data/inflammation-09.csv', 'data/inflammation-10.csv', 'data/inflammation-11.csv', 'data/inflammation-12.csv']

This is has created a list with values of filenames that matched the glob above.


In [6]:
# Lets load two libraries
import numpy                # Fundamental package for scientific computing with Python.
import matplotlib.pyplot    # Provides a MATLAB-like plotting framework.
# this allows plots to appear directly in the notebook
%matplotlib inline

In [7]:
filenames = glob.glob('data/inflammation*.csv')
filenames = filenames[0:3]

In [8]:
filenames


Out[8]:
['data/inflammation-01.csv',
 'data/inflammation-02.csv',
 'data/inflammation-03.csv']

In [9]:
for f in filenames:
    print(f)


data/inflammation-01.csv
data/inflammation-02.csv
data/inflammation-03.csv

In [10]:
for f in filenames:
    print(f)
    
    data = numpy.loadtxt(fname=f, delimiter=',') # load the data in to into the data variable
    fig = matplotlib.pyplot.figure(figsize=(10.0, 3.0)) # create a plotting figure
    
    axes1 = fig.add_subplot(1, 3, 1)  
    axes2 = fig.add_subplot(1, 3, 2)
    axes3 = fig.add_subplot(1, 3, 3)

    axes1.set_ylabel('average')
    axes1.plot(data.mean(axis=0))

    axes2.set_ylabel('max')
    axes2.plot(data.max(axis=0))

    axes3.set_ylabel('min')
    axes3.plot(data.min(axis=0))

    fig.tight_layout()
    matplotlib.pyplot.show()


data/inflammation-01.csv
data/inflammation-02.csv
data/inflammation-03.csv

Creating Functions


In [1]:
def fahr_to_kelvin(temp):
    return ((temp - 32) * (5/9)) + 273.15

In [2]:
fahr_to_kelvin(100)


Out[2]:
310.92777777777775

In [3]:
fahr_to_kelvin(60)


Out[3]:
288.7055555555555

In [9]:
def kelvin_to_celsius(temp_k):
    return temp_k - 273.15

In [10]:
kelvin_to_celsius(310.92777777777775)


Out[10]:
37.77777777777777

In [4]:
def print_hello_world():
    print("Hello World")
    print("Hello everybody")

In [5]:
print_hello_world()


Hello World
Hello everybody

In [7]:
def print_hello_name(name):
    print("Hello %s" % name)

In [8]:
print_hello_name("Andre")


Hello Andre

In [11]:
def get_value_from_input(value):
    if value < 10:
        return value
    else:
        return value * 10

In [12]:
x = get_value_from_input(5)
x


Out[12]:
5

In [13]:
y = get_value_from_input(15)

In [14]:
y


Out[14]:
150

In [ ]: