简体   繁体   中英

Counting the number of times a value appears in a list

This is my starting point. I have separated the specific column of my data which I want to count the specific number of times 0 and 1 appear

print(sr.count(0))

AttributeError: 'Int64Index' object has no attribute 'levels'

I have tried using the above code but it states and as evident the AttributeError shows up.

You have to make it a list first:

print(sr.tolist().count(0))

Then it would be good.

Output:

2

Collections has a Counter class, try:

from collections import Counter
cntr = Counter() 
cntr.update(sr.tolist())

cntr will now have sr as its keys and the count of them as its values. Hope that helps.

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