1

I am attempting to assign bins to a dataframe, I am doing this but creating bins then passing them as an argument but it keeps failing (probably because the dataframe has lists of values instead of values)

I am trying to set the frequencies of a column into bins. Each frequency in the column is a list of frequencies with the following format:

         Freq   Theta
0  191.300003  [-54.0, -52.9999, -52.0001, -51.0]
1  191.929001  [-58.9999, -58.0001, -57.0, -55.9999]

This is not the complete dataset, there are around 15 theta values and they extend from -60 degrees to +60 degrees I try to put them into bins by doing the following:

theta_bins = np.linspace(-60,60,60)
final_lut['binned']=pd.cut(final_lut['Theta'], theta_bins, include_lowest=True)

but when I run this, I encounter the following error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

From further reading up on stack, I encountered this question: 2D array binning and I tried to flatten the Theta values before parsing however the error remains. Any idea why this could be happening and how to fix thetas into the bins of 2 degrees would be greatly appreciated. Thank you!

LeafTeaNeko
  • 113
  • 1
  • 13

1 Answers1

1

You need DataFrame.explode it first if want use pd.cut, because it cannot working with list (like many pandas methods):

df = final_lut.explode('Theta')

theta_bins = np.linspace(-60,60,60)
df['binned']=pd.cut(df['Theta'], theta_bins, include_lowest=True)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252