如何实现Pandas GroupBy对混合类型数据的过滤?

时间:2016-12-20 11:55:33

标签: python python-2.7 pandas dataframe group-by

感谢阅读。对我所确定的问题抱歉是一个简单的问题。

我有一些数据框:

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

我想返回一个新的数据框,其中只包含No Match列中包含Found的条目(并保留其索引顺序),即:

输出:

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

我知道这样做我必须使用内置的Pandas GroupBy()filter()函数。关注这些问题(Filter data with groupby in pandas)和(Pandas: DataFrame filtering using groupby and a function)后,我尝试了:

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

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

谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:3)

您可以调用查询,例如:

,而不是使用groupby函数
df = df[df["Found"] == "No Match"]

因此,如果有Found,它会查找列"No Match",如果是列表,则会False,而不是错误。

相关问题