TableToNumPyArray
汇总
此 TableToNumPyArray 函数用于将表转换为 NumPy 结构化数组。
讨论
NumPy 是用于在 Python 中进行科学计算(包括支持功能强大的多维数组对象)的基础包。 有关详细信息,请参阅 ArcGIS 中的 NumPy 。
要将要素类转换成 NumPy 数组,请改用 FeatureClassToNumPyArray 函数。
语法
TableToNumPyArray(in_table, field_names, {where_clause}, {skip_nulls}, {null_value})
| 参数 | 说明 | 数据类型 |
|---|---|---|
|
in_table |
要素类、图层、表或表视图。 |
String |
|
field_names |
字段名称列表(或组)。 对于单个字段,可以使用一个字符串,而不使用字符串列表。 要访问输入表中的所有字段(BLOB 字段除外),可以使用星号 ( 不支持栅格和 BLOB 字段。 使用 以令牌(如
(默认值为 *) |
String |
|
where_clause |
用于限制所返回的记录的可选表达式。 有关 WHERE 子句和 SQL 语句的详细信息,请参阅 在 ArcGIS 中使用的查询表达式的 SQL 参考 。 (默认值为 "") |
String |
|
skip_nulls |
控制是否跳过使用空值的记录。 可以是布尔值( 设置为 下面是有关跳过所有包含空值的记录的示例。
使用 Python 函数或 使用函数来捕获所有因含空值被跳过的记录。
使用 lambda 表达式来捕获所有因含空值被跳过的记录。
注:在 NumPy 数组中,空值以浮点型(如 (默认值为 False) |
Variant |
|
null_value |
将输入的空值替换为新值。 在计算 Mask
Mask
注意:提供掩膜(例如 -9999)允许将包含空值的整型字段导出到 NumPy 数组,但在任何分析中使用这些值时都请务必谨慎。 分析结果可能因引入的值而意外偏斜。 (默认值为 None) |
Integer |
返回值
| 数据类型 | 说明 |
|---|---|
|
NumPyArray |
NumPy 结构化数组。 |
代码示例
将表转化为 NumPy 数组并为 NumPy 运算基本统计数据。
import arcpy
import numpy
input = "c:/data/usa.gdb/USA/counties"
arr = arcpy.da.TableToNumPyArray(input, ("STATE_NAME", "POP1990", "POP2000"))
# Sum the total population for 1990 and 2000
print(arr["POP1990"].sum())
print(arr["POP2000"].sum())
# Sum the population for the state of Minnesota
print(arr[arr["STATE_NAME"] == "Minnesota"]["POP2000"].sum())
使用 TableToNumPyArray 函数确定两个字段的相关系数。
import arcpy
import numpy
input = arcpy.GetParameterAsText(0)
field1 = arcpy.GetParameterAsText(1)
field2 = arcpy.GetParameterAsText(2)
arr = arcpy.da.TableToNumPyArray(input, (field1, field2))
# Print correlation coefficients for comparison of 2 field values
print(numpy.corrcoef((arr[field1], arr[field2])))