Say I have a dataframe df
a b c
0 False True True
1 False False False
2 True True False
3 False False False
I would like all (index,column) pairs e.g (0,"b"),(0,"c),(2,"a"),(2,"b")
where the True
value is.
Is there a way to do that, without looping over either the index or columns?
CodePudding user response:
Assuming booleans in the input, you can use:
df.where(df).stack().index.to_list()
output:
[(0, 'b'), (0, 'c'), (2, 'a'), (2, 'b')]
CodePudding user response:
Let us try np.where
:
r, c = np.where(df)
[*zip(df.index[r], df.columns[c])]
[(0, 'b'), (0, 'c'), (2, 'a'), (2, 'b')]