Binning column with python pandas

前端 未结 2 1263
礼貌的吻别
礼貌的吻别 2020-11-22 00:36

I have a Data Frame column with numeric values:

df[\'percentage\'].head()
46.5
44.2
100.0
42.12

I want to see the column as bin counts:

2条回答
  •  自闭症患者
    2020-11-22 00:52

    You can use pandas.cut:

    bins = [0, 1, 5, 10, 25, 50, 100]
    df['binned'] = pd.cut(df['percentage'], bins)
    print (df)
       percentage     binned
    0       46.50   (25, 50]
    1       44.20   (25, 50]
    2      100.00  (50, 100]
    3       42.12   (25, 50]
    

    bins = [0, 1, 5, 10, 25, 50, 100]
    labels = [1,2,3,4,5,6]
    df['binned'] = pd.cut(df['percentage'], bins=bins, labels=labels)
    print (df)
       percentage binned
    0       46.50      5
    1       44.20      5
    2      100.00      6
    3       42.12      5
    

    Or numpy.searchsorted:

    bins = [0, 1, 5, 10, 25, 50, 100]
    df['binned'] = np.searchsorted(bins, df['percentage'].values)
    print (df)
       percentage  binned
    0       46.50       5
    1       44.20       5
    2      100.00       6
    3       42.12       5
    

    ...and then value_counts or groupby and aggregate size:

    s = pd.cut(df['percentage'], bins=bins).value_counts()
    print (s)
    (25, 50]     3
    (50, 100]    1
    (10, 25]     0
    (5, 10]      0
    (1, 5]       0
    (0, 1]       0
    Name: percentage, dtype: int64
    

    s = df.groupby(pd.cut(df['percentage'], bins=bins)).size()
    print (s)
    percentage
    (0, 1]       0
    (1, 5]       0
    (5, 10]      0
    (10, 25]     0
    (25, 50]     3
    (50, 100]    1
    dtype: int64
    

    By default cut return categorical.

    Series methods like Series.value_counts() will use all categories, even if some categories are not present in the data, operations in categorical.

提交回复
热议问题