Python 如何测试熊猫系列是否包含在熊猫数据帧的行中
我有一个pandas系列,我想测试它是否与pandas数据帧的一行完全对应 预期行为的例子 df中的Python 如何测试熊猫系列是否包含在熊猫数据帧的行中,python,pandas,dataframe,series,Python,Pandas,Dataframe,Series,我有一个pandas系列,我想测试它是否与pandas数据帧的一行完全对应 预期行为的例子 df中的s==>True 其中s=pd.Series({'a':1,'b':2})和df=pd.DataFrame({'a':[1,2,3],'b':[2,1,3]}) df中的s==>False 其中s=pd.Series({'a':1,'b':2})和df=pd.DataFrame({'a':[2,1,3],'b':[1,3,4]}) 解决方案我知道这是可行的,但很难阅读 不是df[(df['a']=
s
==>True
其中
s=pd.Series({'a':1,'b':2})
和df=pd.DataFrame({'a':[1,2,3],'b':[2,1,3]})
df中的s
==>False
其中
s=pd.Series({'a':1,'b':2})
和df=pd.DataFrame({'a':[2,1,3],'b':[1,3,4]})
解决方案我知道这是可行的,但很难阅读
不是df[(df['a']==s['a'])和(df['b']==s['b'])。空
比较一个想法,然后测试是否至少有一个匹配,然后测试:
此处仅由系列s
为过滤列添加:
df = pd.DataFrame({'a': [1,2,3], 'b': [2,1,3], 'c': [4,5,48]})
print (df)
a b c
0 1 2 4
1 2 1 5
2 3 3 48
s = pd.Series({'a': 1, 'b': 2})
print (df.reindex(s.index, axis=1))
a b
0 1 2
1 2 1
2 3 3
print (df.reindex(s.index, axis=1).eq(s))
a b
0 True True
1 False False
2 False False
print (df.reindex(s.index, axis=1).eq(s).all(axis=1))
0 True
1 False
2 False
dtype: bool
print (df.reindex(s.index, axis=1).eq(s).all(axis=1).any())
True
@user11696358-一件事,它是好的示例数据吗?您是否认为df=pd.DataFrame({'a':[1,2,3],'b':[2,20,3]})
?您是对的示例数据不正确,我更正了它