Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Pandas - create "first fail" column from other column data

Tags:

python

pandas

I have a data frame that represents fail-data for a series of parts, showing which of 3 tests (A, B, C) pass (0) or fail (1).

    A   B   C
1   0   1   1
2   0   0   0
3   1   0   0
4   0   0   1
5   0   0   0
6   0   1   0
7   1   1   0
8   1   1   1

I'd like to add a final column to the dataframe showing the First Fail (FF) of each part, or a default (P) if no fails.

    A   B   C  |  FF
1   0   1   1  |  B
2   0   0   0  |  P
3   1   0   0  |  A
4   0   0   1  |  C
5   0   0   0  |  P
6   0   1   0  |  B
7   1   1   0  |  A
8   1   1   1  |  A

Any easy way to do this pandas? Does it require iterating over each row?

like image 555
joelion Avatar asked Nov 21 '25 00:11

joelion


1 Answers

maybe:

>>> df['FF'] = df.dot(df.columns).str.slice(0, 1).replace('', 'P')
>>> df
   A  B  C FF
1  0  1  1  B
2  0  0  0  P
3  1  0  0  A
4  0  0  1  C
5  0  0  0  P
6  0  1  0  B
7  1  1  0  A
8  1  1  1  A

alternatively:

>>> df['FF'] = np.where(df.any(axis=1), df.idxmax(axis=1), 'P')
>>> df
   A  B  C FF
1  0  1  1  B
2  0  0  0  P
3  1  0  0  A
4  0  0  1  C
5  0  0  0  P
6  0  1  0  B
7  1  1  0  A
8  1  1  1  A
like image 168
behzad.nouri Avatar answered Nov 22 '25 14:11

behzad.nouri