2

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.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
Nn Nn
  • 35
  • 1
  • 3
  • Is that a pandas dataframe? (I'm wondering if those would be relevant tags for this Q.) – TrebledJ Mar 24 '19 at 05:14
  • Possible duplicate of [count the frequency that a value occurs in a dataframe column](https://stackoverflow.com/questions/22391433/count-the-frequency-that-a-value-occurs-in-a-dataframe-column) – awesoon Mar 24 '19 at 05:14

2 Answers2

2

You have to make it a list first:

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

Then it would be good.

Output:

2
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

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.

hd1
  • 33,938
  • 5
  • 80
  • 91