I was trying to make the following script but it gives me an error when executing it, if someone can help me, thank you very much.
I have a list Bxy of 666 elements inside I have a list of 95 elements as shown:
Bxy[0] es una lista [0,0,0,0,0,0,1,0,0,0,...,0] de 95 elementos
.
.
.
Bxy[665] es una lista [0,0,2,0,1,0,4,0,0,0,...,0] de 95 elementos
What I am trying to do is obtain a dataframe in which I obtain the sum of each list, forming a dataframe of 666 elements with the sum of the lists of 95 elements.
When transforming it to np.array I get the following:
np.array(Bxy)
array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=object)
When I do the following it gives me an error.
df = pd.DataFrame()
df["Data"]= list (np.sum(np.array(Bxy), axis=1))
df
TypeError: unsupported operand type(s) for +: 'int' and 'list'
You should get a dataframe with 666 elements.
Data
0
2
0
5
0
1
..
The error itself has no more mystery:
that is, you are trying to add an integer to a list, which is not possible:
The cause is a bit more complicated to find, since in principle what we are trying to do is apply
numpy.sum
to a 2D array of integers that comes from a list of lists of integers of the same length (95 items).What should put us under suspicion is that when printing the array we see:
This to begin with tells us that the array does not contain native NumPy types like int32, but what it contains are native Python objects, which since in Python everything is an object it can be anything from an int, list, a function , etc.
The question is why NumPy hasn't created an array of integers and instead created an array of objects (references). if the constructor of
numpy.array
is passed a list of lists with integers inside it, it will try to create a 2D array of integers, unless there are some circumstances that prevent it:Some item is not an integer and that item cannot be converted to an integer or the integer to the type of that other item (for example, if we mix integers and float, the final array will be of type float).
In the case of lists or other nested containers, if any of the sublists has a different size than the rest, it is impossible to create the NumPy array with NumPy's own types, since within a dimension the number of elements must be constant.
In the specific case of the question, theoretically the original list is a list of lists of 95 integers each, so the result should be a two-dimensional array of type
int64
. But the problem is that there is some item of the sublists (rows) that is not an integer but is a list.If everything in
Bxy
were correct, the code in the question would do what is expected:but if some item is not an integer but a list:
As we can see, a 2D array has been created (because the length of both rows is the same) but of type object, in which each item is either a Python object
int
(native Python type, not NumPy) or a list. When applyingnumpy.sum
on the rows it tries to add7 + [13] + 17
, resulting in the mentioned error.