ValueError: 'object too deep for desired array'

匿名 (未验证) 提交于 2019-12-03 09:02:45

问题:

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) 

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.

回答1:

Try this:

numpy.digitize(df.iloc[:, 0], bins) 

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!