【发布时间】:2021-11-12 20:20:09
【问题描述】:
我有一个熊猫数据框和一个熊猫系列,如下所示。
df0 = pd.DataFrame({'col1':['a','b','c','d'],'col2':['b','c','e','f'],'col3':['d','f','g','a']})
col1 col2 col3
0 a b d
1 b c f
2 c e g
3 d f a
df1 = pd.Series(['b','g','g'], index=['col1','col2','col3'])
col1 b
col2 g
col3 g
dtype: object
如您所见,df0 的列和df1 的索引是相同的。对于df1 的每个索引,我想知道该索引处的值是否存在于df0 的对应列中。所以,df1.col1 是b,我们只需要在df0.col1 中寻找b 并检查它是否存在。
期望的输出:
array([True, False, True])
有没有办法在不使用循环的情况下做到这一点?也许是 numpy 或 pandas 原生的方法?
【问题讨论】:
-
可以使用
melt之类的东西将两个数据帧从宽转换为长,然后join它们在列和值上?
标签: python pandas dataframe numpy