Search code examples
pythonpandasbinning

Assigning Pandas categories to new number


df["A"].value_counts()

(25.0, 38.0]      361594
(12.999, 25.0]    330552
(55.0, 218.0]     305439
(38.0, 55.0]      231683
Name: A, dtype: int64

we have following interval , whenever a new datapoint is coming I need to map to the following above interval . I want something like this .

def func_(x):
    if (x> 12.999) & (x< 25.0):
      return (12.999, 25.0]
    elif:
        for rest of bucket range 

Solution

  • You can reuse bins parameter by categories generated by CategoricalIndex.categories:

    s = df["A"].value_counts()
    
    print (pd.cut(df['new'], bins=s.index.categories))