假设我们有一个如下所示的玩具示例。
np.random.seed(seed=1)
df = pd.DataFrame(np.random.randint(low=0,
high=2,
size=(5, 2)))
df
0 1
0 1 1
1 0 0
2 1 1
3 1 1
4 1 0
我们想要返回所有行的索引,例如某一行。假设我想要像 row 0
这样的所有行的索引,它在 column 0
和 column 1
中都有 1。
我想要一个具有以下内容的数据结构:(0, 2, 3)
。
最佳答案
我认为你可以这样做
df.index[df.eq(df.iloc[0]).all(1)].tolist()
[0, 2, 3]
关于Python Pandas : Return indices of all rows like another row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49225553/