简体   繁体   中英

How do you use pandas isin() function when each row is a list?

For example:

import pandas as pd
data = ['A2', 'A5', 'XS', '2X', '8W']
codes = {'codes':[data]}
df = pd.DataFrame(codes)

                  codes
0  [A2, A5, XS, 2X, 8W]

Now I want to test to see if certain values are in my list from another list.

df['wo'] = df.codes.isin(["8C", "8D", "8E", "8W", "A2"])

I keep getting TypeError: unhashable type: 'list' How do I fix this?

pandas and set

You can use sets. When subtracting sets you get set differences. When comparing sets, you get proper subsets. When a set differenced with another set is a proper subset of that same set... then there was an intersection.

s = df.codes.apply(set)
s - set(["8C", "8D", "8E", "8W", "A2"]) < s

0    True
Name: codes, dtype: bool

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