20

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]
# -> ValueError: setting an array element with a sequence.
2
  • Why do you want to set a list? Commented Oct 19, 2015 at 18:48
  • @AnandSKumar. This is a simple example, by sometimes there will be multiple values so a list makes sense i.e.[400.0, 200.0] Commented Oct 19, 2015 at 18:50

1 Answer 1

26

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.