I am looking to take a dataframe of an image that is binary boolean of False/True's and transform it into an array of coordinates where the data frame is true.
For example, if index[4] and column[8] is true, it would add 4,8 to the array.
IIUC you can do it this way:
In [70]: df
Out[70]:
a b c
0 True False True
1 True True False
2 False True True
3 False True True
4 True False False
5 False True False
6 True False False
7 False True False
8 False False True
9 True False True
In [71]: np.dstack(np.nonzero(df.values))[0]
Out[71]:
array([[0, 0],
[0, 2],
[1, 0],
[1, 1],
[2, 1],
[2, 2],
[3, 1],
[3, 2],
[4, 0],
[5, 1],
[6, 0],
[7, 1],
[8, 2],
[9, 0],
[9, 2]], dtype=int64)
or:
In [76]: np.stack(np.nonzero(df.values)).T
Out[76]:
array([[0, 0],
[0, 2],
[1, 0],
[1, 1],
[2, 1],
[2, 2],
[3, 1],
[3, 2],
[4, 0],
[5, 1],
[6, 0],
[7, 1],
[8, 2],
[9, 0],
[9, 2]], dtype=int64)
Setup:
df = pd.DataFrame(np.random.choice([True, False], (10, 3)), columns=list('abc'))
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