pandas ValueError:使用序列设置数组元素。熊猫

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33221655/
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 00:04:44  来源:igfitidea点击:

ValueError: setting an array element with a sequence. for Pandas

pythonpython-2.7pandas

提问by user2242044

I have a Pandasdataframe, called output. The basic issue is that I would like to set a certain row, column in the dataframeto a list using the ixfunction and am getting ValueError: setting an array element with a sequence.My understanding is that a dataframeelement was like a list element, it could hold anything (string, list, tuple, etc). Am I not correct?

我有一个Pandasdataframe,叫做output。基本问题是我想dataframe使用该ix函数将某个行、列设置为列表,并且ValueError: setting an array element with a sequence.我的理解是一个dataframe元素就像一个列表元素,它可以包含任何东西(字符串、列表、元组等) )。我不正确吗?

Basic setup:

基本设置:

import pandas as pd
output = pd.DataFrame(data = [[800.0]], columns=['Sold Count'], index=['Project1'])
print output.ix['Project1', 'Sold Count']
>>>800

works fine

工作正常

output.ix['Project1', 'Sold Count'] = 400.0
print output.ix['Project1', 'Sold Count']
>>>400.0    

doesn't work

不起作用

output.ix['Project1', 'Sold Count'] = [400.0]
print output.ix['Project1', 'Sold Count']
>>>ValueError: setting an array element with a sequence.

回答by Anand S Kumar

If you really want to set a list as the value for the element, the issue is with the dtypeof the column, when you create the DataFrame, the dtype gets inferred as float64, since it only contains numeric values.

如果您真的想将列表设置为元素的值,问题在于dtype列的 ,当您创建 DataFrame 时,dtype 被推断为float64,因为它只包含数字值。

Then when you try to set a list as the value, it errors out, due to the dtype. A way to fix this would be to use a non-numeric dtype (like object) or so. Example -

然后,当您尝试将列表设置为值时,由于dtype. 解决此问题的一种方法是使用非数字 dtype(如object)左右。例子 -

output['Sold Count'] = output['Sold Count'].astype(object)
output.loc['Project1','Sold Count'] = [1000.0,800.0] #Your list

Demo -

演示 -

In [91]: output = pd.DataFrame(data = [[800.0]], columns=['Sold Count'], index=['Project1'])

In [92]: output
Out[92]:
          Sold Count
Project1         800

In [93]: output['Sold Count'] = output['Sold Count'].astype(object)

In [94]: output.loc['Project1','Sold Count'] = [1000.0,800.0]

In [95]: output
Out[95]:
               Sold Count
Project1  [1000.0, 800.0]


You can also specify the dtypewhile creating the DataFrame, Example -

您还可以dtype在创建 DataFrame 时指定,例如 -

output = pd.DataFrame(data = [[800.0]], columns=['Sold Count'], index=['Project1'],dtype=object)
output.loc['Project1','Sold Count'] = [1000.0,800.0]

Demo -

演示 -

In [96]: output = pd.DataFrame(data = [[800.0]], columns=['Sold Count'], index=['Project1'],dtype=object)

In [97]: output.loc['Project1','Sold Count'] = [1000.0,800.0]

In [98]: output
Out[98]:
               Sold Count
Project1  [1000.0, 800.0]