更新时间:2023-08-28 16:06:22
如果您只寻找一行,那么我可能会做类似的事情
If you're only looking for one row, then I might do something like
>>> 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
并使用此布尔系列从索引中进行选择:
And use this boolean Series to select from the index:
>>> df.index[(df == [3,1,1,0]).all(axis=1)]
Int64Index([2, 3], dtype=int64)
如果您不计算一行的发生,而是想对每一行重复执行此操作,因此您确实想同时定位所有行,那么比一次又一次地执行上述操作要快得多.但这对于一行来说应该已经足够好了.
If you're not counting occurrences of one row, but instead you want to do this repeatedly for each row and so you really want to simultaneously locate all the rows, there are much faster ways than doing the above again and again. But this should work well enough for one row.