简体   繁体   中英

Return numeric value for pandas count function

print(test.query('code == ["A", "B", "C", "D", "E"]') [['code']].count())

This does not return a numeric value that can be compared with a > operator (greater than). How can I change that?

This is why if test.query('code == ["A", "B", "C", "D", "E"]') [['code']].count()>0: will print an error message.

Could someone please help me?

I think problem is your code return one item Series , because [[code]] - it return one column DataFrame.

test = pd.DataFrame({
        'code':list('ABCDEF'),
})

print(test.query('code == ["A", "B", "C", "D", "E"]')[['code']])
  code
0    A
1    B
2    C
3    D
4    E

print(test.query('code == ["A", "B", "C", "D", "E"]') [['code']].count())
code    5
dtype: int64

For scalar change to ['code'] for count of Series :

print (test.query('code == ["A", "B", "C", "D", "E"]')['code'].count())
5

Another idea is get lengths of DataFrame by len :

print (len(test.query('code == ["A", "B", "C", "D", "E"]')))
5

Or using Series.isin and count True s by sum :

print (test['code'].isin(["A", "B", "C", "D", "E"]).sum())
5

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM