将连续行追加到Python数据帧
我想创建一个二维numpy数组 我试过这个:将连续行追加到Python数据帧,python,numpy,Python,Numpy,我想创建一个二维numpy数组 我试过这个: import numpy as np result = np.empty np.append(result, [1, 2, 3]) np.append(result, [4, 5, 9]) print(result.item((1, 2))) 1.数组的尺寸为:(2,3)。我怎样才能得到它们 我试过: print(np.shape(result)) print(np.size(result)) 但这表明: () 1 2.如何访问数组中的特定
import numpy as np
result = np.empty
np.append(result, [1, 2, 3])
np.append(result, [4, 5, 9])
print(result.item((1, 2)))
1.数组的尺寸为:(2,3)
。我怎样才能得到它们
我试过:
print(np.shape(result))
print(np.size(result))
但这表明:
()
1
2.如何访问数组中的特定元素
我试过这个:
import numpy as np
result = np.empty
np.append(result, [1, 2, 3])
np.append(result, [4, 5, 9])
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'
回溯(最近一次呼叫最后一次):
文件“python”,第10行,在
AttributeError:“内置函数”或“方法”对象没有属性“项”
这不是使用numpy
数组的好方法。是一个函数,例如,返回新数组,但忽略返回值
要创建二维阵列,请使用以下命令:
In [3]: result = np.array([[1, 2, 3], [4, 5, 9]])
要找到它的形状:
In [4]: result.shape
Out[4]: (2, 3)
要访问特定元素,请执行以下操作:
In [5]: result[1][2]
Out[5]: 9
这不是使用
numpy
数组的方式。是一个函数,例如,返回新数组,但忽略返回值
要创建二维阵列,请使用以下命令:
In [3]: result = np.array([[1, 2, 3], [4, 5, 9]])
要找到它的形状:
In [4]: result.shape
Out[4]: (2, 3)
要访问特定元素,请执行以下操作:
In [5]: result[1][2]
Out[5]: 9
理想情况下,您应该在交互式会话中测试这类代码,在该会话中,您可以轻松获得有关步骤的更多信息。我将在
ipython
中进行说明
In [1]: result = np.empty
In [2]: result
Out[2]: <function numpy.core.multiarray.empty>
append
创建了一个数组,但请查看内容-函数和3个数字。以及dtype
。另外,np.append
返回一个结果。它不能在适当的地方工作
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'
在这种情况下,np.shape
和np.size
的函数版本不是诊断性的,因为它们首先将result
转换为数组<代码>结果。形状会给出错误
潜在的问题是您使用的是列表模型
result = []
result.append([1,2,3])
result.append([4,5,6])
但是数组append
命名错误,使用不当。它只是np.concatenate
的前端。如果您不理解连接
,您可能不会正确使用np.append。事实上,我认为您根本不应该使用np.append
使用append的正确方法是从尺寸为0维的数组开始,然后重用结果:
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
回到绘图板:
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]])
对于真正的数组,您的项
表达式可以工作,尽管我们通常使用[]
索引:
In [21]: result[1,2]
Out[21]: 6
In [22]: result.item((1,2))
Out[22]: 6
np.append
(注意np.concatenate
的用法)的源代码:
理想情况下,您应该在交互式会话中测试这类代码,在该会话中,您可以轻松获得有关步骤的更多信息。我将在ipython
中进行说明
In [1]: result = np.empty
In [2]: result
Out[2]: <function numpy.core.multiarray.empty>
append
创建了一个数组,但请查看内容-函数和3个数字。以及dtype
。另外,np.append
返回一个结果。它不能在适当的地方工作
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'
在这种情况下,np.shape
和np.size
的函数版本不是诊断性的,因为它们首先将result
转换为数组<代码>结果。形状
会给出错误
潜在的问题是您使用的是列表模型
result = []
result.append([1,2,3])
result.append([4,5,6])
但是数组append
命名错误,使用不当。它只是np.concatenate
的前端。如果您不理解连接
,您可能不会正确使用np.append。事实上,我认为您根本不应该使用np.append
使用append的正确方法是从尺寸为0维的数组开始,然后重用结果:
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
回到绘图板:
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]])
对于真正的数组,您的项
表达式可以工作,尽管我们通常使用[]
索引:
In [21]: result[1,2]
Out[21]: 6
In [22]: result.item((1,2))
Out[22]: 6
np.append
(注意np.concatenate
的用法)的源代码: