How to print a specific information from value_count()?

Viewed 33
import pandas as pd
data = {'qtd':  [0, 1, 4, 0, 1, 3, 1, 3, 0, 0, 
                        3, 1, 3, 0, 1, 1, 0, 0, 1, 3, 
                        0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 
                        0, 1, 1, 1, 1, 3, 0, 3, 0, 0, 
                        2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 
                        0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 
                        2, 0, 0, 2, 0, 0, 2, 0, 0, 1, 
                        1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 
                        0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 
                        1, 1, 1, 1, 1]
          }
df = pd.DataFrame (data, columns = ['qtd'])

Counting

df['qtd'].value_counts()

0    43
1    34
2    10
3     7
4     1
Name: qtd, dtype: int64

What I want is to print a phrase: "The total with zero occurrencies is 43"

Tried with .head(1) but shows more than I want.

2 Answers

Does this solve your problem? The [0] indicates the index you wish to print, in this case the very first occurrence in your column of a data frame.

print('The total with zero occurences is:', df['qtd'].value_counts()[0])

The output of the code above will be:

The total with zero occurences is: 43

I am not sure if you want this but may be helpful:

import inflect
e = inflect.engine()

(df['qtd'].map(e.number_to_words).radd("The total with ").add(" occurances is ")
 .value_counts().astype(str).reset_index().agg(':'.join,1))

0    The total with zero occurances is :43
1     The total with one occurances is :34
2     The total with two occurances is :10
3    The total with three occurances is :7
4     The total with four occurances is :1
dtype: object
Related