【问题标题】:Need to add new column in pandas data frame based on some rule on a particular column需要根据特定列上的某些规则在 pandas 数据框中添加新列
【发布时间】:2019-03-12 11:17:34
【问题描述】:

我在 Pandas 中有一个数据框(使用 Python 3.7),如下所示:

print("DATA FRAME DATA= \n",bin_data_df_sorted.head(5))
# OUTPUT:
# DATA FRAME DATA= 
#     actuals  probability
# 0      0.0     0.116375
# 1      0.0     0.239069
# 2      1.0     0.591988
# 3      0.0     0.273709
# 4      1.0     0.929855

我需要添加名为“bucket”的额外列,以便:

If probability value in between (0,0.1), then bucket=1
If probability value in between (0.1,0.2), then bucket=2
If probability value in between (0.2,0.3), then bucket=3
If probability value in between (0.3,0.4), then bucket=4
If probability value in between (0.4,0.5), then bucket=5
If probability value in between (0.5,0.6), then bucket=6
If probability value in between (0.6,0.7), then bucket=7
If probability value in between (0.7,0.8), then bucket=8
If probability value in between (0.8,0.9), then bucket=9
If probability value in between (0.9,1), then bucket=10

所以,输出应该是这样的:

#     actuals  probability   bucket
# 0      0.0     0.116375      2
# 1      0.0     0.239069      3   
# 2      1.0     0.591988      6
# 3      0.0     0.273709      3
# 4      1.0     0.929855      10

我们该怎么做?

注意:我尝试了下面的代码,但它不能正常工作。

> for val in bin_data_df_sorted['probability']:
>     if val >= 0.0 and val <=0.1:
>         bin_data_df_sorted['bucket']=1
>     elif val > 0.1 and val <=0.2:
>         bin_data_df_sorted['bucket']=2
>     elif val > 0.2 and val <=0.3:
>         bin_data_df_sorted['bucket']=3
and so on.. 

【问题讨论】:

    标签: python pandas data-processing


    【解决方案1】:

    你可以使用pd.cut:

    import numpy as np
    bins = np.arange(0, 1.1, 0.1)
    df['bucket'] = pd.cut(df.probability, bins, labels=(bins*10)[1:])
    
       actuals  probability bucket
    0      0.0     0.116375    2.0
    1      0.0     0.239069    3.0
    2      1.0     0.591988    6.0
    3      0.0     0.273709    3.0
    4      1.0     0.929855   10.0
    

    详情

    pd.cut 将序列中的值分箱到离散间隔中。所以你需要指定一些标准来装箱。你可以这样做:

    bins = np.arange(0,1.1, 0.1)
    # array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
    

    以及返回的 bin 的一些标签,在这种情况下可以使用相同的 bins 生成:

    (bins*10)[1:]
    # array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
    

    【讨论】:

    • 现在,如果我需要绘制一个直方图,使得 x-axis = Bucket Number (i.e 1 to 10) and y-axis = Sum of 'Actuals' 。那我该怎么做呢?
    • 试试df.groupby('bucket').actuals.sum().plot(kind='bar')@Bhuvi007
    猜你喜欢
    • 2020-04-28
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 2016-03-28
    相关资源
    最近更新 更多