ValueError:设置具有序列的数组元素. pandas [英] ValueError: setting an array element with a sequence. for Pandas
问题描述
我有一个Pandas
dataframe
,称为output
.基本问题是我想使用ix
函数将dataframe
中的某一行,列设置为列表,并得到ValueError: setting an array element with a sequence.
我的理解是dataframe
元素就像一个list元素,它可以容纳任何内容(字符串,列表,元组等).我不正确吗?
I have a Pandas
dataframe
, called output
. The basic issue is that I would like to set a certain row, column in the dataframe
to a list using the ix
function and am getting ValueError: setting an array element with a sequence.
My understanding is that a dataframe
element was like a list element, it could hold anything (string, list, tuple, etc). Am I not correct?
基本设置:
import pandas as pd
output = pd.DataFrame(data = [[800.0]], columns=['Sold Count'], index=['Project1'])
print output.ix['Project1', 'Sold Count']
>>>800
工作正常
output.ix['Project1', 'Sold Count'] = 400.0
print output.ix['Project1', 'Sold Count']
>>>400.0
不起作用
output.ix['Project1', 'Sold Count'] = [400.0]
print output.ix['Project1', 'Sold Count']
>>>ValueError: setting an array element with a sequence.
推荐答案
如果您确实想将列表设置为元素的值,则问题在于创建DataFrame时列的dtype
, dtype被推断为float64
,因为它仅包含数字值.
If you really want to set a list as the value for the element, the issue is with the dtype
of the column, when you create the DataFrame, the dtype gets inferred as float64
, since it only contains numeric values.
然后,当您尝试将列表设置为值时,由于dtype
,它会出错.解决此问题的一种方法是使用非数字dtype(例如object
).示例-
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 -
output['Sold Count'] = output['Sold Count'].astype(object)
output.loc['Project1','Sold Count'] = [1000.0,800.0] #Your list
演示-
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]
您还可以在创建DataFrame时指定dtype
,示例-
You can also specify the dtype
while creating the DataFrame, Example -
output = pd.DataFrame(data = [[800.0]], columns=['Sold Count'], index=['Project1'],dtype=object)
output.loc['Project1','Sold Count'] = [1000.0,800.0]
演示-
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]
这篇关于ValueError:设置具有序列的数组元素. pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!