Issue
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?
Solution
Assuming booleans in the input, you can use:
df.where(df).stack().index.to_list()
output:
[(0, 'b'), (0, 'c'), (2, 'a'), (2, 'b')]
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.