Home > Mobile >  Index of row and column which contain specific element
Index of row and column which contain specific element

Time:02-04

I have DataFrame whose row indexes are "A", "B" and column indexes are "C", "D". I would like to pass value (for example bool value True) and get list of pairs of indices (rowIndex,colIndex) which contains the given value. How can I do that in Python?

CodePudding user response:

You can use df.where to mask everything other than your value and then stack it to flatten & get rid of NaNs, and lastly check the index of what remains to get the desired output:

df.where(df.eq(value)).stack().index.tolist()

An example:

>>> df = pd._testing.makeMixedDataFrame()
>>> df
     A    B     C          D
0  0.0  0.0  foo1 2009-01-01
1  1.0  1.0  foo2 2009-01-02
2  2.0  0.0  foo3 2009-01-05
3  3.0  1.0  foo4 2009-01-06
4  4.0  0.0  foo5 2009-01-07

>>> value = 1
>>> df.where(df.eq(value)).stack().index.tolist()
[(1, "A"), (1, "B"), (3, "B")]

Intermediate steps:

>>> df.where(df.eq(value))
     A    B    C   D
0  NaN  NaN  NaN NaT
1  1.0  1.0  NaN NaT
2  NaN  NaN  NaN NaT
3  NaN  1.0  NaN NaT
4  NaN  NaN  NaN NaT

>>> _.stack()
1  A    1.0
   B    1.0
3  B    1.0
dtype: object

>>> _.index
MultiIndex([(1, "A"),
            (1, "B"),
            (3, "B")],
           )
  •  Tags:  
  • Related