【问题标题】:Filter and drop rows by proportion python按比例python过滤和删除行
【发布时间】:2019-03-12 03:29:17
【问题描述】:

我有一个名为 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

【问题讨论】:

    标签: python dataframe filter


    【解决方案1】:

    你可以这样使用:

    df = df[df.proportion >= .01]
    

    从那个数据集中它应该给你这样的东西:

    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
    

    【讨论】:

    • 谢谢,贾布。我需要导入任何东西才能使用 .proportion 吗?
    • 好吧,你从来没有给出列的名称,所以我假设第二列是比例。
    • 啊,我明白了,我添加了其他列
    【解决方案2】:

    想通了

    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))]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 1970-01-01
      • 2014-04-16
      • 1970-01-01
      • 2021-05-06
      相关资源
      最近更新 更多