ValueError:'对象对于所需数组而言太深' [英] ValueError: 'object too deep for desired array'
问题描述
我有一个ValueError:Python程序中对象太深,无法放入所需的数组".
使用numpy.digitize时出现此错误.
我认为这就是我使用Pandas DataFrames的方式:
为了简单起见(因为这是通过外部库完成的),我的程序中有一个列表,但是该库需要一个DataFrame,所以我要执行以下操作:
I have a ValueError: 'object too deep for desired array' in a Python program.
I have this error while using numpy.digitize.
I think it's how I use Pandas DataFrames:
To keep it simple (because this is done through an external library), I have a list in my program but the library needs a DataFrame so I do something like this:
ts = range(1000)
df = pandas.DataFrame(ts)
res = numpy.digitize(df.values, bins)
但是然后看来df.values是一个列表数组,而不是一个浮点数数组.我的意思是:
But then it seems like df.values is an array of lists instead of an array of floats. I mean:
array([[ 0],
[ 1],
[ 2],
...,
[997],
[998],
[999]], dtype=int64)
请帮助,我花了太多时间.
Help please, I spent too much time on this.
推荐答案
尝试一下:
numpy.digitize(df.iloc[:, 0], bins)
您正在尝试从整个DataFrame中获取值.这就是为什么要获得2D阵列的原因.数组中的每一行都是DataFrame的一行.
You are trying to get the values from a whole DataFrame. That is why you get the 2D array. Each row in the array is a row of the DataFrame.
这篇关于ValueError:'对象对于所需数组而言太深'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!