wesm/pandas — Issue #746: Improve docs about filtering
Author: wesm
Created: 2012-02-04 21:22:14+00:00
State: open
Labels: Docs

from mailing list "[pystatsmodels] read_csv + text file with one column"

```
Yes. Suppose that function f returns a boolean value given a column
value. To filter you would do:

df[df[column_to_filter_by].map(f)]

oftentimes filtering is using NumPy vector operations like

df[df[column] == val]

but sometimes you have an element-wise Python function you want to apply.

You said you are a new Python programmer so I can understand lambdas
and regular expressions looking weird =) Lambda is just an alternative
to doing something like:

def condition(x):
return x.startswith('A')

df[df[column].map(condition)]

you could also do:

df[[condition(x) for x in df[column]]]

the map method is just a faster way of doing the list comprehension
(and returns an ndarray with the right data type)

The select method applies a function to the *axis labels*, directly
from the docstring:

In [1]: DataFrame.select?
Type: instancemethod
Base Class:
String Form:
Namespace: Interactive
File: /home/wesm/code/pandas/pandas/core/generic.py
Definition: DataFrame.select(self, crit, axis=0)
Docstring:
Return data corresponding to axis labels matching criteria

Parameters
----------
crit : function
To be called on each index (label). Should return True or False
axis : int

Returns
-------
selection : type of caller

hope this helps,
Wes
```