简体   繁体   中英

Filtering pandas dataframe based on attribute of object in a column

This is something that I can do with a roundabout measure, but I'm wondering if there's something offered by Pandas that makes this possible which might be missing.

So I have a column, "objects", which contains objects that have attributes. One of those attributes is something called "key". I'm trying to filter my dataframe to only include objects whose key belongs in a certain list:

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

The error this returns is AttributeError: 'Series' object has no attribute 'key'

I tried something like this too, but it didn't work:

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

This returns an even more inscrutable error: 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''

Try comparing directly within the lambda function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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