Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于行中的值检索(索引、列)对_Python_Pandas_Indexing - Fatal编程技术网

Python 基于行中的值检索(索引、列)对

Python 基于行中的值检索(索引、列)对,python,pandas,indexing,Python,Pandas,Indexing,我有一个这种格式的数据框 A B C D 1 a a c c 2 c b a a 3 b a c a . . . 我希望使用数据帧操作获得基于行的特定值的所有(索引、列)对。所有(索引、列、行值)对都是唯一的 我注意到这个问题: 虽然这个问题和我的问题完全一样,但这个问题的答案有点模糊,我无法根据这些答案得到我想要的 我也看了一些类似的例子: (a)

我有一个这种格式的数据框

       A    B     C      D

1      a    a     c      c 
2      c    b     a      a 
3      b    a     c      a
.
.
.
我希望使用数据帧操作获得基于行的特定值的所有(索引、列)对。所有(索引、列、行值)对都是唯一的

我注意到这个问题:

虽然这个问题和我的问题完全一样,但这个问题的答案有点模糊,我无法根据这些答案得到我想要的

我也看了一些类似的例子:

(a)

(b)

我试过这个:

req_cols = df.columns [ ( new_df == "a" ).any() ]

req_inds = (df == "a").index

我能够分别获得索引值和列值,但对如何正确组合它们感到困惑

如果我选择行值“a”,我希望得到
[(1,a)、(1,B)、(2,C)、(2,D)、(3,B)、(3,D)]


非常感谢您的帮助。TIA.

的单向,其中
堆栈

df.where(df.eq('a')).stack().index.values
输出:

array([(1, 'A'), (1, 'B'), (2, 'C'), (2, 'D'), (3, 'B'), (3, 'D')],
  dtype=object)