I have the following DataFrame:
index col0 col1 col2
0 0 1 0
1 1 0 1
2 0 1 1
I would like to extract the following indices(those that contain ones(or any value)):
[(0, 1), (1, 0), (1, 2), (2, 1), (2,2))]
Is there a method in pandas that can do this?
duplicated() function Indicate duplicate index values. Duplicated values are indicated as True values in the resulting array. Either all duplicates, all except the first, or all except the last occurrence of duplicates can be indicated.
Finding duplicate rows To find duplicates on a specific column, we can simply call duplicated() method on the column. The result is a boolean Series with the value True denoting duplicate. In other words, the value True means the entry is identical to a previous one.
Use np.where
+ zip
[*zip(*np.where(df))]
[(0, 1), (1, 0), (1, 2), (2, 1), (2, 2)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With