感谢阅读。对我所确定的问题抱歉是一个简单的问题。
我有一些数据框:
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'
谁能告诉我我做错了什么?
答案 0 :(得分:3)
您可以调用查询,例如:
,而不是使用groupby
函数
df = df[df["Found"] == "No Match"]
因此,如果有Found
,它会查找列"No Match"
,如果是列表,则会False
,而不是错误。