python - Pandas - 如果少于 N,则删除唯一行

标签 python pandas

我在 Pandas 数据框中的一列(城市)看起来像:

0           city1
1           city1
2           city2
3           city1
4           city5
5           city2
...

出现个别城市:

city3         6758
city32        6400
city1         4500
city13        2440
...

我想删除出现次数少于 n 次的城市。

最佳答案

我们可以做transform

N=10
newdf=df[df.groupby('city')['city'].transform('count')>N].copy()

关于python - Pandas - 如果少于 N,则删除唯一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58467286/

相关文章:

python - 如何将非硬编码参数传递给Python装饰器?

python - 具有唯一电子邮件的 Django auth.user

python - 按时间和组划分的 Z 分数

python - 尝试循环 DataFrame 中的列并剥离美元符号

python - emacs 飞检 "Configured syntax checker python-flake8 cannot be used"

python - Netflow v9 中的字段 ID 不正确

python - Pandas 创建只有列名的空 DataFrame

python - Pandas:根据条件将数据从 df 提取到新的 df

python - 迭代行并从另一列获取值

python - 根据last_valid_index值过滤pandas DataFrame