【问题标题】:Python Pandas return DataFrame where value count is above a set numberPython Pandas 返回 DataFrame,其中值计数高于设定的数字
【发布时间】:2017-05-12 20:02:36
【问题描述】:

我有一个 Pandas 数据框,并且我只想在该客户编号出现超过设定的次数时才返回该数据框。

以下是 DataFrame 的示例:

114  2017-04-26      1       7507       34      13
115  2017-04-26      3      77314       41      14
116  2017-04-27      7       4525      190     315
117  2017-04-27      7       5525       67      94
118  2017-04-27      1       6525       43     378
119  2017-04-27      3       7415       38      27
120  2017-04-27      2       7613       47      10
121  2017-04-27      2      77314        9       3
122  2017-04-28      1        227       17       4
123  2017-04-28      8       4525      205     341
124  2017-04-28      1       7415       31      20
125  2017-04-28      2      77314        8       2

如果该客户出现超过 5 次,请使用以下代码:

print(zip_data_df['Customers'].value_counts()>5)

7415      True
4525      True
5525      True
77314     True
6525      True
4111      True
227       True
206      False
7507     False
7613     False
4108     False
3046     False
2605     False
4139     False
4119     False

现在我预计如果我这样做了:

print(zip_data_df[zip_data_df['Customers'].value_counts()>5])

它会向我显示出现超过 5 次的客户的整个 DataFrame,但我收到了一个布尔错误。我现在意识到为什么它给了我一个错误:一个 DataFrame 只是告诉我该客户编号是否出现超过 5 次,而另一个每次出现该客户编号时都向我显示。它们的长度不匹配。但是如何获取它,以便数据框仅返回该客户出现超过 5 次的记录?

我确信我忽略了一些简单的答案,但我很感激你能得到我的任何帮助。

【问题讨论】:

  • 我的想法是我需要添加一个列,该列的 custcount 类似于我认为 Abe 建议的,所以我尝试了这个: zip_data_df['cust_counts'] = zip_data_df['Customers'].value_counts( ) 但它只是给了我所有 cust_counts 的“NaN”。

标签: python pandas


【解决方案1】:

我遇到了类似的问题并以这种方式解决了。

cust_counts = zip_data_df['Customers'].value_counts()
cust_list = cust_counts[cust_counts > 5].index.tolist()
zip_data_df = zip_data_df[zip_data_df['Customers'].isin(cust_list)]

【讨论】:

  • 优秀的直接解决方案!
【解决方案2】:

您可以在此处使用方便的小 groupby 转换来完成工作

subset_customers_df = zip_data_df[
          zip_data_df.groupby('Customers')
         ['Customers'].transform('size')>5]

适用于 Pandas 0.25.3

【讨论】:

    【解决方案3】:

    我相信您正在寻找的是:

    zip_data_df['Customers'].value_counts()[zip_data_df['Customers'].value_counts()>5]
    

    【讨论】:

    • 问题是只返回出现超过 5 次的客户,但如果该客户出现超过 5 次,我想返回整行(所有字段)。
    【解决方案4】:

    所以这里的问题是索引:value_counts() 返回一个索引在“客户”上的系列,而 zip_data_df 似乎索引在其他东西上。您可以执行以下操作:

    cust_counts = zip_data_df['Customers'].value_counts().rename('cust_counts')
    
    zip_data_df = zip_data_df.merge(cust_counts.to_frame(),
                                    left_on='Customers',
                                    right_index=True)
    

    从那里,您可以像这样从 zip_data_df 有条件地选择:

    zip_data_df[zip_data_df.cust_counts > 5]
    

    【讨论】:

    • 我从那个解决方案中得到这个错误:ValueError: can not merge DataFrame with instance of type
    • @Emac ok 在合并中,将第一个参数 cust_counts 替换为 cust_counts.to_frame()
    【解决方案5】:

    没试过,但这应该可以:

    cust_by_size = zip_data_df.groupBy("Customers").size()
    cust_index_gt_5 = cust_by_size.index[cust_by_size > 5]
    zip_data_cust_index_gt_5 = zip_data_df[zip_data_df["Customers"].isin(cust_index_gt_5)]
    

    【讨论】:

      【解决方案6】:
      # create the boolean series 
      s1 = pd.series(zip_data_df['Customers'].value_counts()) 
      
       # select rows that are greater than 5
      result = [i for i,j in zip(s1.index,s1.value) if j>5]
      
       # select rows based values in the result list 
      zip_data_df[zip_data_df.Customers.isin(result)]
      

      这对我有用,虽然写这么长的代码很蹩脚,但比oneliners更容易理解

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-15
        • 2020-08-19
        • 1970-01-01
        • 1970-01-01
        • 2020-03-30
        • 2020-02-06
        • 2021-03-27
        相关资源
        最近更新 更多