pandas ValueError: '对象对于所需数组来说太深'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39474056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:00:17  来源:igfitidea点击:

ValueError: 'object too deep for desired array'

pythonpandasnumpydataframe

提问by Othman Nejjar

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:

我在 Python 程序中有一个 ValueError: 'object too deep for required array'。使用 numpy.digitize 时出现此错误。
我认为这就是我使用 Pandas DataFrames 的方式:
为了简单起见(因为这是通过外部库完成的),我的程序中有一个列表,但该库需要一个 DataFrame,所以我执行以下操作:

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:

但是看起来 df.values 是一个列表数组而不是一个浮点数组。我的意思是:

array([[   0],
   [   1],
   [   2],
   ..., 
   [997],
   [998],
   [999]], dtype=int64)

Help please, I spent too much time on this.

请帮助,我花了太多时间在这上面。

采纳答案by Kartik

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.

您正在尝试从整个 DataFrame 中获取值。这就是为什么你会得到二维数组。数组中的每一行都是 DataFrame 的一行。