Python 使用pandas元素作为字典中的键
我有一本字典:Python 使用pandas元素作为字典中的键,python,pandas,dictionary,Python,Pandas,Dictionary,我有一本字典: fdict={“X”:['tf'、'pytorch'、'keras']、“Y”:['Gym'、'Scikit']} 以及带有列c1和c2的数据帧df: c1 c2 比索 Y长 X迈斯特 我想得到: fdict[df['c1']]中的'pytorch'作为布尔响应,在这种情况下,它将是真的与lambda函数和get一起使用,输出为布尔值系列: m = df['c1'].apply(lambda x: 'pytorch' in fdict.get(x, None)) print (m
fdict={“X”:['tf'、'pytorch'、'keras']、“Y”:['Gym'、'Scikit']}
以及带有列c1
和c2
的数据帧df
:
真的与lambda函数和get
一起使用,输出为布尔值系列
:
m = df['c1'].apply(lambda x: 'pytorch' in fdict.get(x, None))
print (m)
0 True
1 False
2 True
Name: c1, dtype: bool
如果要测试是否至少有一个True
添加:
m1 = df['c1'].apply(lambda x: 'pytorch' in fdict.get(x, None)).any()
print (m1)
True