Reputation: 3
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.sstatic.net/pcPkS.jpg
Output [enter image description here][2] https://i.sstatic.net/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.sstatic.net/pcPkS.jpg [2]: https://i.sstatic.net/hHt0W.jpg
Please click on the image to check the input and output
Upvotes: 0
Views: 58
Reputation: 28640
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)
Upvotes: 0