【问题标题】:iPython: Using Pandas to count words, how do I count the least occuring?iPython:使用 Pandas 计算单词,我如何计算最少出现的单词?
【发布时间】:2016-05-26 19:30:24
【问题描述】:

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

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

如何编码以计算出现次数最少的单词?

【问题讨论】:

  • 你数数然后倒序排列。

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


【解决方案1】:

更新:

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]

【讨论】:

  • 无论如何将其限制为出现的最小值?我只想要出现一次的单词。
  • @JetCorey,我已经更新了我的答案 - 请检查。这就是你想要的吗?
【解决方案2】:

我认为你可以使用 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

或者在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[dft_counts == 1])
【解决方案3】:

只需对系列进行排序:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-22
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    相关资源
    最近更新 更多