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

Python Dataframe categorize values

I have a data coming from the field and I want to categorize it with a gap of specific range.
I want to categorize in 100 range. That is, 0-100, 100-200, 200-300
My code:

df=pd.DataFrame([112,341,234,78,154],columns=['value'])

    value
0   112
1   341
2   234
3   78
4   154

Expected answer:

    value  value_range
0   112    100-200
1   341    200-400
2   234    200-300
3   78     0-100
4   154    100-200

My code:

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

df['value_range'] = df['value'].apply(lambda x:[a,b] if x>a and x<b for a,b in zip([0,100,200,300,400],[100,200,300,400,500]))

Present solution:

SyntaxError: invalid syntax

>Solution :

You can use pd.cut:

df["value_range"] = pd.cut(df["value"], [0, 100, 200, 300, 400], labels=['0-100', '100-200', '200-300', '300-400'])
print(df)

Prints:

   value value_range
0    112     100-200
1    341     300-400
2    234     200-300
3     78       0-100
4    154     100-200
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