'How to add column in python dataframe based on range of another column?

I have a df like category, x. I need another columns x-range which is assigned labels based on what range column x in.

category x
1 25
1 56
3 74
3 23

| output | |

category x xrange
1 25 (56 - 25)
1 56 (56 -25)
3 74 (74 -23)
3 23 (74 - 23)

Basically new to python and tried using max and min but not working.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source