gpt4 book ai didi

python Pandas : Return indices of all rows like another row

转载 作者:行者123 更新时间:2023-11-28 22:20:04 26 4
gpt4 key购买 nike

假设我们有一个如下所示的玩具示例。

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 0column 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com