python - 根据分类列中不同值的计数从 pandas 数据框中删除所有行

标签 python python-3.x pandas

我使用了包含许多列的汽车数据框。如果我得到某些制造商的出现次数:

carsDF.manufacturer.value_counts()

结果类似于:

 VW               2228
 Opel             1414
 Renault          1362
 Audi              895
 BMW               888
 Mercedes-Benz     786

如果某个制造商的出现总数小于特定数量,如何从数据框中删除该制造商的所有行?

最佳答案

你可以做一个 map :

# get the count for each manufacturer
counts = carsDF.manufacturer.value_counts()

# threshold
thresh = 1000

# replace the manufacturer with the counts and thresholding
carsDF[carsDF.manufacturer.map(counts).ge(thresh)]

关于python - 根据分类列中不同值的计数从 pandas 数据框中删除所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58314135/

相关文章:

python-3.x - Pandas:检查一列是否存在于另一列中

Python删除方括号和它们之间的无关信息

python - 运行 python 代码给出 NameError : name 'ABCMeta' is not defined every time

python - 值错误: Cannot use name of an existing column for indicator column

python - 使用条件修改 pandas 数据框中的值

python - 具有关联对象的同一张表上的多对多关系

python - pandas `to_numeric` 整数向下转换 float 不为整数

python - TensorFlow 服务 RAM 使用情况

python - 为什么 AdaBoostRegressor 的 neg_mean_squared_error 为正数?

python - 使用字典作为键划分两个 Pandas DataFrame