Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way of extracting indices from a pandas DataFrame based on value [duplicate]

Tags:

python

pandas

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?

like image 537
cristian hantig Avatar asked Oct 18 '19 13:10

cristian hantig


People also ask

Can index have duplicate values pandas?

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.

What is a correct method to discover if a row is a duplicate pandas?

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.


1 Answers

Use np.where + zip


[*zip(*np.where(df))]

[(0, 1), (1, 0), (1, 2), (2, 1), (2, 2)]
like image 61
user3483203 Avatar answered Oct 13 '22 01:10

user3483203