繁体   English   中英

根据列中 object 的属性过滤 pandas dataframe

[英]Filtering pandas dataframe based on attribute of object in a column

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

所以我有一列“对象”,其中包含具有属性的对象。 这些属性之一是称为“键”的东西。 我正在尝试过滤我的 dataframe 以仅包含其键属于某个列表的对象:

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 function 内直接比较:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM