根据列中对象的属性筛选panda数据帧



这是我可以用迂回措施来做的事情,但我想知道Pandas是否提供了一些可能缺失的东西。

所以我有一个专栏,";对象";,其包含具有属性的对象。这些属性之一是所谓的";键";。我试图过滤我的数据帧,只包括密钥属于某个列表的对象:

df2 = df[df["object"].key.isin(list_of_keys)]

返回的错误是AttributeError: 'Series' object has no attribute 'key'

我也试过这样的东西,但没有用:

df2 = df[df["object"].map(lambda x: x.key).isin(list_of_keys)]

这将返回一个更加难以理解的错误:TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

尝试直接在lambda函数中进行比较:

df2 = df[df["object"].map(lambda x: x.key in list_of_keys)]

最新更新