0

Simple dataframe in pandas:

A  B  C  
1  1  
1  0  
0  1  
0  0  

Say I'd like to populate column C with a 1 if A and B both are 1, else 0. If I try to do something like:

    if df['A'] == 1 and df['B'] == 1:
        df['C'] = 1
    else:
        df['C'] = 0

I get something like:

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Which code will be the most efficient and how should it be used?

piRSquared
  • 285,575
  • 57
  • 475
  • 624
kozowh
  • 93
  • 2
  • 10

1 Answers1

1

Multiply

df.assign(C=df.A * df.B)

   A  B  C
0  1  1  1
1  1  0  0
2  0  1  0
3  0  0  0

all

Don't do this

df.assign(C=df[['A', 'B']].astype(bool).all(1).astype(int))

   A  B  C
0  1  1  1
1  1  0  0
2  0  1  0
3  0  0  0
piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • Thanks for the suggestions, @piRSquared. Maybe I didn't frame the problem adequately. I wanted to use a conditional statement on two columns to generate a third as in the example I gave. You have solved the "and" problem but I also need a solution to the "or" problem such as: ` if A == 1 or B == 1 then C = 1` – kozowh Sep 12 '18 at 15:18
  • @kozowh the permutations of various logical combinations are infinite. It is a lot of effort to attempt to answer while covering a high level of generality. You could produce an example that adequately covers what you are really trying to do. – piRSquared Sep 12 '18 at 15:20
  • Thanks for your comment. I did manage to find a solution to this issue thanks to Stack Overflow members. Here are the general forms. `df['STEC'] = [(1 if i or j == 1 else 0) for i,j in zip(df['stx2'], df['stx1'])] df['EHEC'] = [(1 if i and j == 1 else 0) for i,j in zip(df['eae'], df['STEC'])] df['O157'] = [(1 if (i or j == 1) and k == 1 else 0) for i,j,k in zip(df['rfb'],df['LF'], df['EHEC'])]` These seem to work! – kozowh Sep 13 '18 at 17:22