如何更新计数大于x的值

2024-04-20 11:18:31 发布

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

我有一个pandas列,它包含了很多少于5次的字符串,我不想删除这些值,但是我想用一个名为“pruned”的占位符字符串替换它们。最好的方法是什么?你知道吗

df= pd.DataFrame(['a','a','b','c'],columns=["x"])
# get value counts and set pruned I want something that does as follows
df[df[count<2]] = "pruned"

Tags: columnsand方法字符串dataframepandasdfget
2条回答

我怀疑有一种更有效的方法可以做到这一点,但简单的方法是构建一个计数dict,然后在这些值低于计数阈值时进行修剪。以df为例:

df= pd.DataFrame([12,11,4,15,6,12,4,7],columns=['foo'])

    foo
0   12
1   11
2   4
3   15
4   6
5   12
6   4
7   7

# make a dict with counts
count_dict = {d:(df['foo']==d).sum() for d in df.foo.unique()}
# assign that dict to a column
df['bar'] = [count_dict[d] for d in df.foo]
# loc in the 'pruned' tag
df.loc[df.bar < 2, 'foo']='pruned'

按需返回:

    foo bar
0   12      2
1   pruned  1
2   4       2
3   pruned  1
4   pruned  1
5   12      2
6   4       2
7   pruned  1

(当然,如果需要,您可以将2改为5并转储bar列)。你知道吗

更新

对于每个就地版本的请求,这里有一个一行程序,它可以在不指定另一列或直接创建dict的情况下完成(感谢@trumonaminima提供的values_count()提示):

df= pd.DataFrame([12,11,4,15,6,12,4,7],columns=['foo'])
print(df)
df.foo = df.foo.apply(lambda row: 'pruned' if (df.foo.value_counts() < 2)[row] else row)
print(df)

根据需要再次返回:

   foo
0   12
1   11
2    4
3   15
4    6
5   12
6    4
7    7
      foo
0      12
1  pruned
2       4
3  pruned
4  pruned
5      12
6       4
7  pruned

这是我最后使用的基于上述答案的解决方案。你知道吗

import pandas as pd
df= pd.DataFrame([12,11,4,15,6,12,4,7],columns=['foo'])
# make a dict with counts
count_dict = dict(df.foo.value_counts())
# assign that dict to a column
df['temp_count'] = [count_dict[d] for d in df.foo]
# loc in the 'pruned' tag
df.loc[df.temp_count < 2, 'foo']='pruned'
df = df.drop(["temp_count"], axis=1)

相关问题 更多 >