2024-06-16 10:01:44 发布
网友
使用iPython3。我能计算出一列中出现次数最多的单词
import pandas as pd dft = pd.read_csv('NYC.txt') dft_counts = complaints['Provider'].value_counts() dft_counts[:10]
我怎样才能把它编码成最少出现的单词?你知道吗
更新:
counts = complaints['Provider'].value_counts() counts[counts == 1]
显示小于或等于3的“计数”:
counts[counts <= 3]
旧答案:
你可以这样做:
complaints['Provider'].value_counts().nsmallest(1)
或者您可以使用iloc定位器,这可能要快一点:
iloc
complaints['Provider'].value_counts().iloc[-1]
我认为可以使用^{}和-1what返回最后一个值,因为最后一个值最小-^{}排序Serie:
-1
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:
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])
更新:
显示小于或等于3的“计数”:
旧答案:
你可以这样做:
或者您可以使用
iloc
定位器,这可能要快一点:我认为可以使用^{} 和} 排序
-1
what返回最后一个值,因为最后一个值最小-^{Serie
:如果需要所有最小值,请使用^{} :
或者在
value_counts
中使用参数ascending=True
:只需对序列进行排序:
相关问题 更多 >
编程相关推荐