python - 按比例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
解决方案
弄清楚了
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))]
推荐阅读
- vba - 创建跟踪配额达到的订单日志
- jsp - 如何根据jsp下拉列表中选择的值获取文本框中的值
- r - R - 组内求和,并且仅当另一个变量具有连续值时
- sql - 在 MS Access 中使用选项组时如何修复“对象不支持此属性或方法”错误?
- react-native - expo 我们需要两个谷歌地图 api 密钥吗?
- java - java.lang.StackOverflowError:使用 gson 序列化对象时为 null
- mysql - 如何选择每组中具有最大值的第一行?
- html - Button error in Firefox but not in Chrome
- c# - Capping out when looping through every email in a folder
- gnuradio - 无法在 GNU Radio 中检测到 B200