Search code examples
pythonlistfftbinning

Binning task: I have a list of frequencies which I need to put in bins, facing errors


I have df['freq'], this is a list of numbers stored as ['0.0','0.3','0.9]. My task is to classify them into 30 bins(from 0.0, 0.1,0.2...... till 3.0)

  1. I have used the ravel function to flatten it

  2. Made a new_list for storing these strings as float

     def binning_function(col,cut_points,labels=None):
     minval = 0.0
     maxval = 3.0
     break_points = [minval] + cut_points + [maxval]
     print(break_points)
     if not labels:
         labels = range(len(cut_points) + 1)
    
     colbin = pd.cut(col,bins=break_points, labels=labels,include_lowest = True ) 
    
     return colbin
    

Please refer to this code snippet: Please refer to this code snippet

The error is

'<' not supported between instances of 'float' and 'list'


Solution

  • You are appending the list x_elem instead of each value. As a result, the program is comparing a list and a number which is not allowed. So append each value by modifying the for loop.

    for x_elem in range(len(flat)):
        x_elem = df_ft['freq'].iloc[_].strip("[]").split(", ")
        x_elem = list2float(x_elem)
        for elem in x_elem:
            new_list.append(elem)