iPython:使用Pandas计算单词,我如何计算最少的单词?

时间:2016-05-26 19:30:24

标签: python python-3.x pandas ipython anaconda

使用iPython3。我能够弄清楚如何计算列中最常出现的单词

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

如何对此进行编码以计算最少发生的单词?

3 个答案:

答案 0 :(得分:3)

<强>更新

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

显示“计数”小于或等于3:

counts[counts <= 3]

OLD回答:

你可以这样做:

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

或者你可以使用iloc定位器,这可能会更快一点:

complaints['Provider'].value_counts().iloc[-1]

答案 1 :(得分:1)

我认为您可以iat使用-1返回最后一个值,因为最后一个值最小 - value_counts排序Serie

dft_counts.iat[-1]

如果需要所有最小值,请使用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

或者在ascending=True中使用参数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 :(得分:0)

对系列进行排序:

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