如果你只寻找一行,那么我可能会做类似的事情
>>> df.index[(df == [3, 1, 1, 0]).all(axis=1)]
Int64Index([2, 3], dtype=int64)
--
解释如下。从...开始:
>>> df
A B C D
0 1 1 2 0
1 3 3 2 1
2 3 1 1 0
3 3 1 1 0
4 3 3 2 1
5 1 2 3 4
我们与我们的目标进行比较:
>>> df == [3,1,1,0]
A B C D
0 False True False True
1 True False False False
2 True True True True
3 True True True True
4 True False False False
5 False False False False
找到匹配的:
>>> (df == [3,1,1,0]).all(axis=1)
0 False
1 False
2 True
3 True
4 False
5 False
并使用此布尔系列从索引中进行选择:
>>> df.index[(df == [3,1,1,0]).all(axis=1)]
Int64Index([2, 3], dtype=int64)
如果您不计算一行的出现次数,而是想对每一行重复执行此操作,并且确实想同时定位所有行,那么有比一次又一次执行上述操作更快的方法。但这对于一行来说应该足够好了。