Chuck
Chuck

Reputation: 3852

How to implement Pandas GroupBy filter on mixed type data?

Thanks for reading. Apologies for what I am sure is a simple problem to answer.

I have some dataframe:

df:
    Entry    Found
0    Dog      [1,0]
1    Sheep    [0,1]
2    Cow      "No Match"
3    Goat     "No Match"

I want to return a new dataframe which contains only entries which contain No Match in the Found column (and preserve their index order) i.e.:

Output:

    Entry    Found
0    Cow      "No Match"
1    Goat     "No Match"

I know to do this I must use the built in Pandas GroupBy() and filter() functions. Following these questions (Filter data with groupby in pandas) and (Pandas: DataFrame filtering using groupby and a function) I tried:

>> df.groupby('Found','Entry').filter(lambda x: type(x) == str)
>> No axis named Entry for object type <class 'pandas.core.frame.DataFrame'>

and:

>> df.groupby('Found').filter(lambda x: type(x) == str)
>> TypeError: unhashable type: 'list'

Can anyone tell me what I am doing wrong?

Upvotes: 3

Views: 336

Answers (1)

ebeneditos
ebeneditos

Reputation: 2612

Instead of using the groupby function, you can call the query such as:

df = df[df["Found"] == "No Match"]

Thus it will look for the column Found if there are "No Match", which will be False when it is a list, instead of an error.

Upvotes: 3

Related Questions