将数据框与值列表合并时的 ValueError

ValueError in binning the dataframe with lists of values

我正在尝试将 bins 分配给数据框,我正在这样做但是创建了 bins 然后将它们作为参数传递但是它一直失败(可能是因为数据框有值列表而不是值)

我正在尝试将列的频率设置到 bin 中。列中的每个频率都是具有以下格式的频率列表:

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

这不是完整的数据集,大约有 15 个 theta 值,范围从 -60 度到 +60 度 我尝试通过执行以下操作将它们放入垃圾箱:

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

但是当我运行这个的时候,我遇到了下面的错误:

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

通过进一步阅读堆栈,我遇到了这个问题: 我试图在解析之前展平 Theta 值,但错误仍然存​​在。 任何想法为什么会发生这种情况以及如何将 thetas 固定到 2 度的箱子中将不胜感激。谢谢!

如果要使用 pd.cut,首先需要 DataFrame.explode,因为它不能与 list 一起使用(就像许多 pandas 方法一样):

df = final_lut.explode('Theta')

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