python-3.x pandas:如何使value_count()值高于阈值

r3i60tvu  于 2023-08-08  发布在  Python
关注(0)|答案(3)|浏览(123)

我怎样才能让value_counts高于阈值?我试过了

df[df[col].value_counts(dropna=False) > 3]

字符串
得到所有大于3的计数,但我得到

IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).


有什么提示吗?谢谢

vs3odd8k

vs3odd8k1#

试试看:

df[df.groupby(col)[col].transform('size')>3]

字符串
value_counts

counts = df[col].value_counts(dropna=False) 
valids = counts[counts>3].index

df[df[col].isin(valids)]


value_countsmap的另一种方法:

counts = df[col].value_counts(dropna=False)
df[df[col].map(counts)>3]

bfnvny8b

bfnvny8b2#

尝试使用isin并与原始value_counts链接

out = df[df.col.isin(df[col].value_counts(dropna=False).loc[lambda x : x>3].index)].copy()

字符串
也可以试试filter

out = df.groupby(col).filter(lambda x : len(x)>3)

bweufnob

bweufnob3#

坚持value_counts,这里有一个简单的解决方案:

df[col].value_counts(dropna=False)[df[col].value_counts(dropna=False) > 3]

字符串

相关问题