Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading