Archive for the 'Python' Category

Page 2 of 6

Test the speed of your code interactively in IPython

So you’ve come up with a couple of ways to solve a problem in code. But how do you decide which way is the best? One criterion to decide on is to use the one that makes the most sense to you. Another criterion is to use the version that is fastest. Here’s how to quickly determine which way is fastest using the interactive interpreter IPython. Continue reading ‘Test the speed of your code interactively in IPython’

Sort one list by another list

Here are a couple of ways of sorting one list by another list in Python. The first uses plain ol’ Python, and the others use NumPy.

In each case imagine we want to sort a list of peoples names by their ages. Continue reading ‘Sort one list by another list’

Read Excel files from Python

Use the excellent xlrd package, which works on any platform. That means you can read Excel files from Python in Linux! Example usage:

Open the workbook

import xlrd
wb = xlrd.open_workbook('myworkbook.xls')

Check the sheet names

wb.sheet_names()

Get the first sheet either by index or by name

sh = wb.sheet_by_index(0)
sh = wb.sheet_by_name(u'Sheet1')

Iterate through rows, returning each as a list that you can index:

for rownum in range(sh.nrows):
    print sh.row_values(rownum)

If you just want the first column:

first_column = sh.col_values(0)

Index individual cells:

cell_A1 = sh.cell(0,0).value
cell_C4 = sh.cell(rowx=3,colx=2).value

(Note Python indices start at zero but Excel starts at one)

Turns out the put_cell() method isn’t supported, so ignore the following section (Thanks for the heads up, John!)

Put something in the cell:

row = 0
col = 0
ctype = 1  # see below
value = 'asdf'
xf = 0  # extended formatting (use 0 to use default)
sh.put_cell(row, col, ctype, value, xf)
sh.cell(0,0)  # text:u'asdf'
sh.cell(0,0).value  # 'asdf'

Possible ctypes: 0 = empty, 1 = string, 2 = number, 3 = date, 4 = boolean, 5 = error

MySQLdb – accessing MySQL databases from Python

MySQL is a popular open-source database engine, and Python interfaces quite nicely with MySQL with the MySQLdb package. For more on why you would want to use a database for your data, check out this post. Here I’ll show you how to connect to your existing MySQL database with Python. Continue reading ‘MySQLdb – accessing MySQL databases from Python’

Convert None to NaN for use in NumPy arrays

I originally had the function below to convert values of None into values of NaN, but realized it is much faster and easier to coerce the array type:

x.astype(float)

Here’s the slow way to do it:

from numpy import array, nan
def None2NaN(x):
    """Converts None objects to nan, for use in
       NumPy arrays. Returns an array."""
    newlist = []
    for i in x:
        if i is not None:
            newlist.append(i)
        else:
            newlist.append(nan)
    return array(newlist)

“Vectorization” in NumPy

How do you get Matlab-like vectorization when using NumPy? The key is using parentheses when using logical operators. Here’s an example:

from numpy import *

a = array([1,2,3,4,5])
b = array([5,4,3,2,1])
c = array([1,2,2,2,1])

# ===The right way===

(a<5) & (b>3) & (c==2)
# array([ False,  True, False, False, False], dtype=bool)

# ===The wrong way===

a<5 & b>3 & c==2
# ValueError:
# The truth value of an array with more than
# one element is ambiguous. Use a.any() or a.all()

Alternatively, use NumPy’s logical_and and logical_or functions . . . but these functions only take two arguments at a time.

“unexpected indent” errors in Python

If you’ve used Python, you know that whitespace is used to delimit blocks of code.

But whitespace comes in two different flavors: tabs and spaces, and you have to pay attention to which one you are using: Continue reading ‘“unexpected indent” errors in Python’

Check the type of array in Numpy

from numpy import array
a = array([1,2,3])
a.dtype  # dtype('int32')
a.dtype.kind  # 'i', for 'integer'

s = array(['a','b','c'])
s.dtype  # dtype('|S1')
s.dtype.kind  # 'S' for 'string'

f = array([1., 2., 3.])
f.dtype  # dtype('float64')
f.dtype.kind  # 'f' for 'float'

List files in a directory with Python

List files in C:/work:

import os
files = os.listdir('C:/work')

To list the files in the current working directory,

import os
files = os.listdir(os.getcwd())

Interactively select points from a plot in matplotlib

In Matlab, there is a very useful function called ginput(). There is no such ready-built function for matplotlib/pylab, but all the pieces are there. Copy and paste this code to get ginput functionality.
Continue reading ‘Interactively select points from a plot in matplotlib’