首页 > 解决方案 > 按比例python过滤和删除行

问题描述

我有一个名为 wine 的数据框,其中包含我需要删除的一堆行。

如何删除“国家”列中小于整体 1% 的所有行?

以下是比例:

#proportion of wine countries in the data set
wine.country.value_counts() / len(wine.country)

US                        0.382384
France                    0.153514
Italy                     0.100118
Spain                     0.070780
Portugal                  0.062186
Chile                     0.056742
Argentina                 0.042835
Austria                   0.034767
Germany                   0.028928
Australia                 0.021434
South Africa              0.010233
New Zealand               0.009069
Israel                    0.006133
Greece                    0.004493
Canada                    0.002526
Hungary                   0.001755
Romania                   0.001558

...我变得懒惰并且没有包括所有结果,但我想你明白我的意思。我需要删除所有比例小于 0.01 的行

这是我的数据框的头部:

country designation points  price   province    taster_name     variety     year    price_category
Portugal  Avidagos   87     15.0    Douro       Roger Voss  Portuguese Red  2011.0  low

标签: pythondataframefilter

解决方案


弄清楚了

country_filter = wine.country.value_counts(normalize=True) > 0.01
country_index = country_filter[country_filter.values == True].index
wine = wine[wine.country.isin(list(country_index))]

推荐阅读