Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

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.
like image 456
user2242044 Avatar asked Oct 19 '15 18:10

user2242044


People also ask

What is Valueerror setting an array element with a sequence?

ValueError: setting an array element with a sequence. Means exactly what it says, you're trying to cram a sequence of numbers into a single number slot. It can be thrown under various circumstances.

How do you fix error setting an array element with a sequence?

Easiest way to fix this problem is to use the data-type which support all type of data-type. Second way to fix this problem is to match the default data-type of array and assigning value.

How do you assign an element to an array in Python?

Adding elements to an Array using array moduleUsing + operator: a new array is returned with the elements from both the arrays. append(): adds the element to the end of the array. insert(): inserts the element before the given index of the array. extend(): used to append the given array elements to this array.


1 Answers

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.

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

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 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]

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]
like image 155
Anand S Kumar Avatar answered Oct 06 '22 14:10

Anand S Kumar