将连续的行追加到Python数据框 [英] Appending successive rows to Python dataframe
问题描述
我想创建一个二维的numpy数组.
I want to create a bidimensional numpy array.
我尝试过:
import numpy as np
result = np.empty
np.append(result, [1, 2, 3])
np.append(result, [4, 5, 9])
1.数组的尺寸为:(2, 3)
.我怎样才能得到它们?
1.The dimensions of the array are: (2, 3)
. How can I get them?
我尝试过:
print(np.shape(result))
print(np.size(result))
但这会打印:
()
1
2.如何访问数组中的特定元素?
2.How can I access a specific element in the array?
我尝试过:
print(result.item((1, 2)))
但这返回:
Traceback (most recent call last):
File "python", line 10, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'item'
推荐答案
理想情况下,您应该在交互式会话中测试此类代码,在该会话中您可以轻松地获得有关步骤的更多信息.我将在ipython
中进行说明.
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>
这是一个函数,而不是数组.正确的用法是result = np.empty((3,))
.那就是您必须使用所需的size参数调用该函数.
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
创建了一个数组,但请查看内容-函数和3个数字.还有dtype
.同样,np.append
返回结果.它不能就地工作.
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'
您的错误告诉我们result
是一个函数,而不是数组.您一开始就设置了相同的内容.
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)
在这种情况下,np.shape
和np.size
的功能版本无法诊断,因为它们首先将result
转换为数组. result.shape
会给出错误.
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])
但是数组append
被错误命名和滥用.它只是np.concatenate
的前端.如果您不理解concatenate
,则可能不会正确使用np.append
.实际上,我认为您根本不应该使用np.append
.
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.
使用append的正确方法是从尺寸为0的数组开始,然后重用结果:
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])
但是,那不是您想要的吗?甚至我在滥用append
.
But maybe that isn't what you want? Even I'm misusing append
.
返回到绘图板:
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]])
使用真实数组,您的item
表达式可以工作,尽管通常我们使用[]
索引:
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
np.append
的源代码(请注意使用np.concatenate
):
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)
这篇关于将连续的行追加到Python数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!