0

Like df.isnull().sum() for counting Nan value, I m looking to count another value : 'MISSED'.

Does anybody know if there is any build-in fonction in pandas to do that, or how to do that ?

b0chData
  • 3
  • 2

1 Answers1

1

Use values for numpy boolen array and sum all Trues:

df = pd.DataFrame({'A':list('abcdef'),
                   'B':[4,5,4,5,5,'missed'],
                   'C':['missed',8,9,4,2,3],
                   'D':[1,3,5,7,1,0],
                   'E':['missed',3,6,9,2,'missed'],
                   'F':list('aaabbb')})

print (df)
   A       B       C  D       E  F
0  a       4  missed  1  missed  a
1  b       5       8  3       3  a
2  c       4       9  5       6  a
3  d       5       4  7       9  b
4  e       5       2  1       2  b
5  f  missed       3  0  missed  b

count = (df.values == 'missed').sum()
print (count)
4
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • @StefanFalk - it is [`values`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.values.html) - convert to `numpy array` – jezrael May 23 '18 at 09:32
  • Just `(df == 'missed').sum()` also works fine. `(df == 'missed').sum().sum()` if we want the total `missed` of the whole dataframe. – phi May 23 '18 at 09:45
  • when i use {.isnull().sum()}, that return a pandas Series : column / count but when i use {values} i have juste the general sum (int64) – b0chData May 23 '18 at 09:52
  • @b0chData - Then use `count = (df.astype(str)== 'missed').sum()` – jezrael May 23 '18 at 10:10
  • exactly what i want thank you – b0chData May 23 '18 at 10:29