2

Using iPython3. I was able to figure out how to count the most occurring words in a column

import pandas as pd
dft = pd.read_csv('NYC.txt')
dft_counts = complaints['Provider'].value_counts()
dft_counts[:10]

How can I code this to count the least occurring word?

1
  • You count and then reverse the list.
    – Merlin
    May 27, 2016 at 6:47

3 Answers 3

3

UPDATE:

counts = complaints['Provider'].value_counts()
counts[counts == 1]

show "counts" less or equal than 3:

counts[counts <= 3]

OLD answer:

you can do it this way:

complaints['Provider'].value_counts().nsmallest(1)

alternatively you can use iloc locator, which might be bit faster:

complaints['Provider'].value_counts().iloc[-1]
2
  • anyway to limit it to the smallest values that occur? I only want the words that occur once.
    – JetCorey
    May 27, 2016 at 19:36
  • 1
    @JetCorey, i've updated my answer - please check. Is that what you want? May 27, 2016 at 19:47
1

I think you can use iat with -1 what return last value, because last value is smallest - value_counts sorts Serie:

dft_counts.iat[-1]

If need all smallest values use boolean indexing:

dft_counts = (s.value_counts())
print (dft_counts)
6       3
5       3
null    2
18      1
3       1
22      1
0       1
dtype: int64

print (dft_counts.iat[-1])
1

print (dft_counts[dft_counts == dft_counts.iat[-1]])
18    1
3     1
22    1
0     1
dtype: int64

Alternatively use parameter ascending=True in value_counts:

dft_counts = (s.value_counts(ascending=True))
print (dft_counts)
0       1
22      1
3       1
18      1
null    2
5       3
6       3
dtype: int64

print (dft_counts[:3])
0     1
22    1
3     1
dtype: int64
2
  • anyway to limit it to the smallest values that occur? I only want the words that occur once.
    – JetCorey
    May 27, 2016 at 19:36
  • Then you can use (dft_counts[dft_counts == 1])
    – jezrael
    May 27, 2016 at 19:37
0

Just sort the Series:

dft_counts = complaints['Provider'].value_counts()
dft_counts.sort_values(["Provider"], ascending=[True])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.