How to find row and column of an item in a Dataframe

Viewed 517

I have a DataFrame like this:

  A    B     C
0  True  True  False
1  True  True  True
2  False True True

I want to look for the instances of False and get its row and column.

Expected result: [(0, 'C'), (2, 'A')]. not exactly on this data structure format, but you get the drill.

I've tried doing this:

df[df.eq(False).any(1)]

What It does is filter out the rows where there's no False bool.

Any tips ?

1 Answers

Use DataFrame.stack for MultiIndex Series and then filter indices by inverted values by ~ for match Falses:

s = df.stack()
L = s.index[~s].tolist()
print (L)
[(0, 'C'), (2, 'A')]

Or get indices for Falses in numpy.where and zip values for list of tuples:

i, c = np.where(~df)

L = list(zip(df.index[i], df.columns[c]))
print (L)
[(0, 'C'), (2, 'A')]
Related