Find the first true value in a Pandas dataframe string

I have two data frames with boolean values.

The first is as follows:

b1=pd.DataFrame([[ True, False, False, False, False],
       [False, False,  True, False, False],
       [False,  True, False, False, False],
       [False, False, False, False, False]])

b1
Out[88]: 
       0      1      2      3      4
0   True  False  False  False  False
1  False  False   True  False  False
2  False   True  False  False  False
3  False  False  False  False  False

If I'm just interested in whether each row has any true value, I can use the method any:

b1.any(1)
Out[89]: 
0    True
1    True
2    True
3    False
dtype: bool

However, I want to have an added constraint based on a second data frame that looks like this:

b2 = pd.DataFrame([[ True, False,  True, False, False],
       [False, False,  True,  True,  True],
       [ True,  True, False, False, False],
       [ True,  True,  True, False, False]])

b2
Out[91]: 
       0      1      2      3      4
0   True  False   True  False  False
1  False  False   True   True   True
2   True   True  False  False  False
3   True   True   True  False  False

I want to identify rows that have a true value in the first data frame ONLY if this is the first true value in the row of the second data block.

, 2, , , . , 1 2 dataframe 1, 2. :

0    True
1    True
2    False
3    False
dtype: bool
+4
3

cumsum, :

In [123]: (b1 & b2 & (b2.cumsum(axis=1) == 1)).any(axis=1)
Out[123]: 
0     True
1     True
2    False
3    False
dtype: bool

, b2.cumsum(axis=1) Trues, , 1, b2 - True, .

In [124]: b2.cumsum(axis=1)
Out[124]: 
   0  1  2  3  4
0  1  1  2  2  2
1  0  0  1  2  3
2  1  2  2  2  2
3  1  2  3  3  3
+6

, pshep123.

# the part on the right of & is to check if the first True position in b1 matches the first True position in b2.

b1.any(1) & (b1.values.argmax(axis=1) == b2.values.argmax(axis=1))
Out[823]: 
0     True
1     True
2    False
3    False
dtype: bool
+1

As an option for @DSM's smart answer, this approach seemed to me more intuitive. The first part should be pretty straightforward, and the second part will find the first column number (w / axis = 1), which is true for each data frame and is compared.

(b1.any(axis = 1) & (b1.idxmax(axis = 1) == b2.idxmax(axis = 1))
0
source

Source: https://habr.com/ru/post/1677048/


All Articles