I was revising some old code and stumbled upon this snippet.
df = pd.DataFrame({
'x': ['a', 'b', 'c', 'd', 'e', 'dc', 'ca', 'cd', 'cf', 'cv', 'cs', 'ca', 'ac', 'fc'],
'a': [34, 28, 51,5,120,12,45,56,67,54,34,32,1213,2]})
five = df[df['a'] < 5]
ten = df[(df['a'] > 5) & (df['a'] < 10)]
twenty = df[(df['a'] > 10) & (df['a'] < 20 )]
thirty = df[(df['a'] > 20) & (df['a'] < 30 )]
forty = df[(df['a'] > 30) & (df['a'] < 40 )]
fifty = df[(df['a'] > 40) & (df['a'] < 50 )]
sixty = df[(df['a'] > 50) & (df['a'] < 60)]
over = df[(df['a'] > 60)]
Basically there is a DataFrame and I need to group values that are within a certain range. There are multiple ranges. The grouped values are later used in a box plot. The code above gets the job done but I am pretty sure there is a better way to do this!
Question: What if I need to create 1000 groups? I want to change the edge values and add new groups more dynamically. How can this be done?