bsky
bsky

Reputation: 20222

Appending successive rows to Python dataframe

I want to create a bidimensional numpy array.

I tried this:

import numpy as np

result = np.empty
np.append(result, [1, 2, 3])
np.append(result, [4, 5, 9])

1.The dimensions of the array are: (2, 3). How can I get them?

I tried:

print(np.shape(result))
print(np.size(result))

But this prints:

()
1

2.How can I access a specific element in the array?

I tried this:

print(result.item((1, 2)))

But this returns:

Traceback (most recent call last):
  File "python", line 10, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'item'

Upvotes: 1

Views: 257

Answers (2)

hpaulj
hpaulj

Reputation: 231395

Ideally you should be testing this sort of code in an interactive session, where you can easily get more information on the steps. I'll illustrate in ipython.

In [1]: result = np.empty
In [2]: result
Out[2]: <function numpy.core.multiarray.empty>

This is a function, not an array. The correct use is result = np.empty((3,)). That is you have to call the function with a desired size parameter.

In [3]: np.append(result, [1,2,3])
Out[3]: array([<built-in function empty>, 1, 2, 3], dtype=object)

append has created an array, but look at the contents - the function and 3 numbers. And the dtype. Also np.append returns a result. It does not work in-place.

In [4]: result.item((1,2))
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-51f2b4be4f43> in <module>()
----> 1 result.item((1,2))

AttributeError: 'builtin_function_or_method' object has no attribute 'item'

Your error tells us that result is a function, not an array. The same thing you set at the start.

In [5]: np.shape(result)
Out[5]: ()
In [6]: np.array(result)
Out[6]: array(<built-in function empty>, dtype=object)

In this case the function versions of np.shape and np.size aren't diagnostic, because they first convert result into an array. result.shape would have given an error.

The underlying problem is that you are using a list model

result = []
result.append([1,2,3])
result.append([4,5,6])

But the array append is misnamed, and misused. It is just a front end to np.concatenate. If you don't understand concatenate, you probably won't use np.append right. In fact, I would argue that you shouldn't use np.append at all.


The correct way to use append is to start with an array that has size 0 dimension, and reuse the result:

In [7]: result = np.empty((0,3),int)
In [8]: result
Out[8]: array([], shape=(0, 3), dtype=int32)
In [9]: result = np.append(result,[1,2,3])
In [10]: result
Out[10]: array([1, 2, 3])
In [11]: result = np.append(result,[4,5,6])
In [12]: result
Out[12]: array([1, 2, 3, 4, 5, 6])

But maybe that isn't what you want? Even I'm misusing append.

Back to the drawing board:

In [15]: result = []
In [16]: result.append([1,2,3])
In [17]: result.append([4,5,6])
In [18]: result
Out[18]: [[1, 2, 3], [4, 5, 6]]
In [19]: result = np.array(result)
In [20]: result
Out[20]: 
array([[1, 2, 3],
       [4, 5, 6]])

With a real array, your item expression works, though usually we use [] indexing:

In [21]: result[1,2]
Out[21]: 6
In [22]: result.item((1,2))
Out[22]: 6

Source code for np.append (note the use of np.concatenate):

In [23]: np.source(np.append)
In file: /usr/local/lib/python3.5/dist-packages/numpy/lib/function_base.py

def append(arr, values, axis=None):
    """
    ...
    """
    arr = asanyarray(arr)
    if axis is None:
        if arr.ndim != 1:
            arr = arr.ravel()
        values = ravel(values)
        axis = arr.ndim-1
    return concatenate((arr, values), axis=axis)

Upvotes: 3

Ami Tavory
Ami Tavory

Reputation: 76297

That's not quite the way to use numpy arrays. empty is a function. append, for example, returns new arrays, but you're ignoring the return value.

To create the 2-d array, use this:

In [3]: result = np.array([[1, 2, 3], [4, 5, 9]])

To find its shape:

In [4]: result.shape
Out[4]: (2, 3)

To access a specific element:

In [5]: result[1][2]
Out[5]: 9

Upvotes: 1

Related Questions