Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 827
SRJCoding Avatar asked Oct 14 '25 14:10

SRJCoding


1 Answers

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
like image 140
Andrej Kesely Avatar answered Oct 17 '25 04:10

Andrej Kesely