1

I would like to count the number of a claim where claimnb=0 and bm=1. This is my code for first argument but how to add the second condition?

(data['ClaimNb'] == 0).sum()
L200
  • 11
  • 1
  • 5

1 Answers1

1

Assuming you are using Numpy, try using & to combine the 2 conditions:

((data['ClaimNb'] == 0) & (data['bm'] == 1)).sum()

Make sure you add the parentheses around each condition.

See How to use NumPy where with multiple conditions in Python for more information about combining conditions in Numpy expressions.

Matthew R.
  • 615
  • 4
  • 12
  • These are masks.. are you guys summing boolean values? – adir abargil Nov 29 '20 at 20:26
  • I believe Numpy implicitly converts boolean to integers when applying numerical operations, so booleans can be summed to count the total number of True elements. The implicit conversion also means you can do a element-wise bitwise AND on the boolean arrays to combine multiple condition statements. – Matthew R. Nov 29 '20 at 20:30
  • I don't think that was his intention.. he wants to sum the balues that stand in this conditions... this is also the duplicate answer to his question.. – adir abargil Nov 29 '20 at 20:33
  • thank you guys, but this didn't work – L200 Nov 29 '20 at 20:40