【问题标题】:How to bin data in pandas dataframe [duplicate]如何在 Pandas 数据框中对数据进行 bin 处理 [重复]
【发布时间】:2021-08-14 07:57:34
【问题描述】:

amount 列分箱到不同的桶中并获取每个桶的长度的更有效的方法是什么。

桶是amount

1. amount < 10000
2. amount >=10000 & <100000
3. amount >100000 & <500000
4. amount > 500000

我试图使用以下方法实现上述问题:

sample_data = 
       date    amount type
0   2018-09-28  4000.0  D
1   2018-11-23  2000.0  D
2   2018-12-27  52.5    D
3   2018-10-02  20000.0 D
4   2018-11-27  4000.0  C
5   2018-06-01  500.0   D
6   2018-07-02  5000.0  D
7   2018-07-02  52.5    D
8   2018-10-31  500.0   D
9   2018-11-26  2000.0  C

sample_data['Transactions_bin_1'] = sample_data[sample_data.amount < 10000]['amount']
sample_data['Transactions_bin_2'] = sample_data[(sample_data['amount'] >= 10000) & (sample_data['amount'] < 100000)]['amount']
sample_data['Transactions_bin_3'] = sample_data[(sample_data['amount'] >= 100000) & (sample_data['amount'] < 500000)]['amount']
sample_data['Transactions_bin_4'] = sample_data[sample_data.amount > 500000]['amount']

bin_classification =

{
    
    'bin1' : sample_data.Transactions_bin_1.count(),
    'bin2' :  sample_data.Transactions_bin_2.count(),
    'bin3' : sample_data.Transactions_bin_3.count(),
    'bin4' :  sample_data.Transactions_bin_4.count()
}

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    使用pd.cut:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.cut.html:

    import numpy as np
    bins = [-np.inf,10000,100000,500000,np.inf]
    labels = ['amount < 10000' ,'amount >=10000 & <100000','amount >=100000 & <500000', 'amount >= 500000']
    df['bins'] = pd.cut(df.amount, bins=bins, , labels=labels,  right=False, include_lowest=True)
    

    OUTPUT:

             date   amount type                      bins
    0  2018-09-28   4000.0    D            amount < 10000
    1  2018-11-23   2000.0    D            amount < 10000
    2  2018-12-27     52.5    D            amount < 10000
    3  2018-10-02  20000.0    D  amount >=10000 & <100000
    4  2018-11-27   4000.0    C            amount < 10000
    5  2018-06-01    500.0    D            amount < 10000
    6  2018-07-02   5000.0    D            amount < 10000
    7  2018-07-02     52.5    D            amount < 10000
    8  2018-10-31    500.0    D            amount < 10000
    9  2018-11-26   2000.0    C            amount < 10000
    

    注意:如果需要,您可以操作 labels 列表。

    【讨论】:

    • IntervalIndex 相同。 +1。 (OP想要每个间隔的长度)。
    • @Nk03 pd.cut 将amount == 10000 分类为&lt; 10000。同样适用于100000500000
    • @okoliechukwuka 将修复它。谢谢。 :)
    • @Corralien。哦,我错过了。我认为 OP 只需要 bin 标签。
    【解决方案2】:

    使用pd.cutpd.IntervalIndex.from_breaks

    >>> pd.cut(df["amount"], closed="left",
               bins=pd.IntervalIndex.from_breaks([0, 10000, 100000, 500000,  np.inf], \
          .value_counts()
    
    [0.0, 10000.0)          9
    [10000.0, 100000.0)     1
    [100000.0, 500000.0)    0
    [500000.0, inf)         0
    Name: amount, dtype: int64
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-01
      • 2018-09-06
      • 1970-01-01
      • 2017-02-17
      • 2019-03-30
      • 1970-01-01
      • 2019-03-07
      • 2016-07-03
      相关资源
      最近更新 更多