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 (ie offers 111, 333, and 444). Does anyone know how to do this please?
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:
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
.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
len()
:print(len(x))
Or: sum the mask:
print(mask.sum())
Prints:
3
在一行中:
my_count =len(df[(df.Country=='UK') && (df.Type in ['A','B','C']) && (df.Cancelled=='N')])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.