我有一个数据框:
d = {'col1':['2q','48', '48-49']}
test = pd.DataFrame(d)
col1
0 2q
1 48
2 48-49
还有一个映射字典:
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
}
我想测试测试数据帧的每个值,看看它是否存在于 mupcs_to_pc_mapping 字典值中。
所以最终的结果是这样的:
0 False
1 True
2 True
我尝试了以下变体:
test['col1'].isin(mupcs_to_pc_mapping.values())
和
test['col1'].any(value for value in mupcs_to_pc_mapping.values())
但两者都会导致错误。
有人知道我的代码有什么问题吗?
最佳答案
你也可以这样做:
test.isin(pd.DataFrame(mupcs_to_pc_mapping).stack().values)
col1
0 False
1 True
2 True
关于python - 将数据框与字典值列表进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49366585/