How to create a column dynamically when you have multiple values in one clolumn

Question:

I want to create a column based upon another column which have values like 0,25,50,75,100 and so on . So I want to create a dynamic code so that no need to define manually.

So suppose I have a data frame with 10000 rows

Input
[enter image description here][1]
https://i.stack.imgur.com/pcPkS.jpg

Output
[enter image description here][2]
https://i.stack.imgur.com/hHt0W.jpg

So basically I want 0-25 as 1 , 25 to 50 as 2 , 50 to 75 as 3 and so on

Please help me out how to do this in a dynamic way
[1]: https://i.stack.imgur.com/pcPkS.jpg
[2]: https://i.stack.imgur.com/hHt0W.jpg

Please click on the image to check the input and output

Asked By: Ashish Dhakad

||

Answers:

Could use pandas .cut()

import pandas as pd


data = [0,25,50,75,100,125,150]

df = pd.DataFrame(data, columns=['A'])


lower = 0
upper = max(df['A'])
bins = range(lower, upper+1, 25)


df['B'] = pd.cut(x=df['A'], bins=bins, include_lowest=True, right=True)
bins = list(set(df['B']))
bins.sort()
bin_label = {k: idx for idx, k in enumerate(bins, start=1)}

df['C'] = df['B'].map(bin_label)
Answered By: chitown88
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.