Python Pandas : Return indices of all rows like another row

标签 python pandas indexing

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

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/

相关文章:

python - 从 Python 守护进程认真打印到标准输出

python - Pandas 使用 str 列进行分配

python - 来自mysql数据库python的Group Pivot Pandas数据

sql - 表数据库上的索引

sql - 搜索时CONVERT()对INDEX有什么影响?

python - 在 Python 中通过网络发送对象的最佳方式是什么?

python - 从较小的原始列表中返回 N 个随机项目

Python:新对象使用先前对象的属性

Python复制具有相同ID值的行

indexing - 我应该索引主键的列部分吗