1

I want to group some list of number by creating a function that converts list of numbers into group then convert these numbers into pandas dataframe but it is not working.

List = [ 10, 50, 80, 12, 5, 8, 19, 90, 40, 8, 7, 9, 18, 27, 30, 45]

def number_group(x):

  item = [ ]
  for i in x:
    if i in range(0, 30):
      print (' group 10 and 30')  
    elif i in range(30, 60):
      print ('group 30 and 60')
    elif i in range (60, 90):
      print ('group 60 and 90')
    elif i in range (90, 120):
      print ('group 90 and 120')
    else:
      return ('NAN')
    item.append((i))
  return 

When i pass my the list into the function and try converting the result into panda dataframe i keep getting none. Any idea how these can be done?

smci
  • 32,567
  • 20
  • 113
  • 146
  • (Don't use capitals in variable names and don't shadow the builtin `list`. So best to name your list `ll` or something.) – smci Jul 31 '21 at 22:03
  • This is called ***binning*** and the command is `pd.cut(x, bins)` – smci Jul 31 '21 at 22:07

2 Answers2

0

list=number_group([ 10, 50, 80, 12, 5, 8, 19, 90, 40, 8, 7, 9, 18, 27, 30,45])

df=pd.DataFrame(list,columns=['Number_Group'])

Chadc
  • 53
  • 4
0

This is called binning and the command is pd.cut(x, bins). As a bonus it gives you NaNs for out-of-range values:

ll = [10, 50, 80, 12, 5, 8, 19, 90, 40, 8, 7, 9, 18, 27, 30, 45, -1, 333]
binned = pd.cut(ll, [0, 30, 60, 90, 120], ordered=True)

[(0, 30], (30, 60], (60, 90], (0, 30], (0, 30], ..., (0.0, 30.0], (0.0, 30.0], (30.0, 60.0], NaN, NaN]

Length: 18
Categories (4, interval[int64]): [(0, 30] < (30, 60] < (60, 90] < (90, 120]]

Note the output is a pandas Categorical. And it gives the categories names similar to what you want.

smci
  • 32,567
  • 20
  • 113
  • 146