根据列中对象的属性过滤熊猫数据框

时间:2020-12-30 02:42:11

标签: python pandas

这是我可以通过迂回措施来做的事情,但我想知道 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''

1 个答案:

答案 0 :(得分:1)

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

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