groupby
does something else entirely. It creates groups for aggregation. Basically, it goes from something like:
['a', 'b', 'a', 'c', 'b', 'b']
to something like:
[['a', 'a'], ['b', 'b', 'b'], ['c']]
What you want is df.apply
.
In newer versions of pandas
there's a query
method that makes this a bit more efficient and easier.
However, one what of doing what you want is to make a boolean array by using
mask = df.Type.apply(lambda x: 'Fruit' in x)
And then selecting the relevant portions of the data frame with df[mask]
. Or, as a one-liner:
df[df.Type.apply(lambda x: 'Fruit' in x)]
As a full example:
import pandas as pd
data = [['Orange', 'Edible, Fruit'],
['Banana', 'Edible, Fruit'],
['Tomato', 'Edible, Vegtable'],
['Laptop', 'Non Edible, Electronic']]
df = pd.DataFrame(data, columns=['Item', 'Type'])
print df[df.Type.apply(lambda x: 'Fruit' in x)]