SRJCoding
SRJCoding

Reputation: 473

How to count rows in a dataframe that satisfy multiple conditions?

I have a dataframe (lets call it df) that looks a bit like this.

Offer | Country | Type | Cancelled
------|---------|------|----------
111   | UK      | A    | N
222   | UK      | A    | Y
333   | UK      | B    | N
444   | UK      | C    | N
555   | UK      | D    | N
666   | UK      | E    | N 
777   | ROI     | A    | N

 

I want to get a variable to hold a count of all the UK offers that fall into types A, B, or C, and which have not been cancelled. So with the data above the variable would be set to 3 (i.e offers 111, 333, and 444). Does anyone know how to do this please?

Upvotes: 4

Views: 5809

Answers (2)

ibarrond
ibarrond

Reputation: 7591

In one line:

my_count =len(df[(df.Country=='UK') && (df.Type in ['A','B','C']) && (df.Cancelled=='N')])

Upvotes: 1

Andrej Kesely
Andrej Kesely

Reputation: 195418

Try:

x = df.loc[
    df.Country.eq("UK") & df.Type.isin(["A", "B", "C"]) & df.Cancelled.eq("N")
]
print(len(x))

Prints:

3

Step-by-step:

  1. Create a mask:
mask = (
    df.Country.eq("UK") & df.Type.isin(["A", "B", "C"]) & 
df.Cancelled.eq("N")
)


0     True
1    False
2     True
3     True
4    False
5    False
dtype: bool
  1. Use .loc with the mask:
x = df.loc[mask]


   Offer Country Type Cancelled
0    111      UK    A         N
2    333      UK    B         N
3    444      UK    C         N
  1. Use len():
print(len(x))

Or: sum the mask:

print(mask.sum())

Prints:

3

Upvotes: 6

Related Questions