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

时间:2015-10-19 18:44:56

标签: python python-2.7 pandas

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

基本设置:

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.

1 个答案:

答案 0 :(得分:12)

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

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

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,示例 -

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]