ValueError: setting an array element with a sequence

后端 未结 8 1188
萌比男神i
萌比男神i 2020-11-22 05:46

This Python code:

import numpy as p

def firstfunction():
    UnFilteredDuringExSummaryOfMeansArray = []
    MeanOutputHeader=[\'TestID\',\'         


        
相关标签:
8条回答
  • 2020-11-22 06:40

    From the code you showed us, the only thing we can tell is that you are trying to create an array from a list that isn't shaped like a multi-dimensional array. For example

    numpy.array([[1,2], [2, 3, 4]])
    

    or

    numpy.array([[1,2], [2, [3, 4]]])
    

    will yield this error message, because the shape of the input list isn't a (generalised) "box" that can be turned into a multidimensional array. So probably UnFilteredDuringExSummaryOfMeansArray contains sequences of different lengths.

    Edit: Another possible cause for this error message is trying to use a string as an element in an array of type float:

    numpy.array([1.2, "abc"], dtype=float)
    

    That is what you are trying according to your edit. If you really want to have a NumPy array containing both strings and floats, you could use the dtype object, which enables the array to hold arbitrary Python objects:

    numpy.array([1.2, "abc"], dtype=object)
    

    Without knowing what your code shall accomplish, I can't judge if this is what you want.

    0 讨论(0)
  • 2020-11-22 06:41

    In my case, I had a nested list as the series that I wanted to use as an input.

    First check: If

    df['nestedList'][0]
    

    outputs a list like [1,2,3], you have a nested list.

    Then check if you still get the error when changing to input df['nestedList'][0].

    Then your next step is probably to concatenate all nested lists into one unnested list, using

    [item for sublist in df['nestedList'] for item in sublist]
    

    This flattening of the nested list is borrowed from How to make a flat list out of list of lists?.

    0 讨论(0)
提交回复
热议问题