Reputation: 45
I have a dataframe:
d = {'col1':['2q','48', '48-49']}
test = pd.DataFrame(d)
col1
0 2q
1 48
2 48-49
And a dictionary for mapping:
mupcs_to_pc_mapping = {'24': ['48-49', '48', '49'], #M2
'23': ['50-51', '50', '51'], #M3
'22': ['52-53', '52', '53'], #M4
'21': ['54-55', '54', '55'], #M5
}
I would like to test each value of the test dataframe to see if it exists in the mupcs_to_pc_mapping dict values.
So the final result is something like:
0 False
1 True
2 True
I tried a variation of:
test['col1'].isin(mupcs_to_pc_mapping.values())
and
test['col1'].any(value for value in mupcs_to_pc_mapping.values())
but both of them result in an error.
Does anyone know what is wrong with my code?
Upvotes: 2
Views: 1774
Reputation: 294278
Create a set that is the union of all dictionary values
test.col1.isin(set.union(*map(set, mupcs_to_pc_mapping.values())))
0 False
1 True
2 True
Name: col1, dtype: bool
Upvotes: 3
Reputation: 323266
I think you need
test.col1.isin(sum(mupcs_to_pc_mapping.values(),[]))
Out[477]:
0 False
1 True
2 True
Name: col1, dtype: bool
Mentioned by cold in the comment itertools.chain
list(itertools.chain.from_iterable(mupcs_to_pc_mapping.values()))
Upvotes: 4
Reputation: 402523
First, invert your mapping:
m = {v : k for k in mupcs_to_pc_mapping for v in mupcs_to_pc_mapping[k]}
Now, use map
and test for NaN
s.
test.col1.map(m).notnull()
0 False
1 True
2 True
Name: col1, dtype: bool
Upvotes: 3
Reputation: 30605
You can also do :
test.isin(pd.DataFrame(mupcs_to_pc_mapping).stack().values)
col1
0 False
1 True
2 True
Upvotes: 5