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

2024-06-16 10:01:44 发布

您现在位置:Python中文网/ 问答频道 /正文

使用iPython3。我能计算出一列中出现次数最多的单词

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

我怎样才能把它编码成最少出现的单词?你知道吗


Tags: csvimporttxtpandasreadasprovider单词
3条回答

更新:

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

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

counts[counts <= 3]

旧答案:

你可以这样做:

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

或者您可以使用iloc定位器,这可能要快一点:

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

我认为可以使用^{}-1what返回最后一个值,因为最后一个值最小-^{}排序Serie

dft_counts.iat[-1]

如果需要所有最小值,请使用^{}

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

或者在value_counts中使用参数ascending=True

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

只需对序列进行排序:

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

相关问题 更多 >