# Convert group of string to panda DataFrame [duplicate]

Solution for Convert group of string to panda DataFrame [duplicate]
is Given Below:

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?

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’])

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.