TableToNumPyArray (arcpy.da)

摘要

将表转换为 NumPy 结构化数组。

讨论

FeatureClassToNumPyArray 函数与 TableToNumPyArray 函数类似。FeatureClassToNumPyArray 函数也为处理要素数据提供了附加参数。

NumPy 是 Python 中用于进行科学计算的基础包,其包括支持功能强大的 N 维数组对象。有关详细信息,请参阅在 ArcGIS 中使用 NumPy

语法

TableToNumPyArray (in_table, field_names, {where_clause}, {skip_nulls}, {null_value})
参数说明数据类型
in_table

要素类、图层、表或表视图。

String
field_names
[field_names,...]

字段名称列表(或组)。对于单个字段,可以使用一个字符串,而不使用字符串列表。

如果要访问输入表中的所有字段(栅格和 BLOB 字段除外),可以使用星号 (*) 代替字段列表。但是,为了获得较快的性能和可靠的字段顺序,建议您将字段列表限制在实际需要的字段。

不支持日期、几何、栅格和 BLOB 字段。

以令牌(如 OID@)取代字段名称可访问更多的信息:

  • OID@Returns the value of the ObjectID field.

(默认值为 *)

String
where_clause

用于限制所返回的记录的可选表达式。有关 WHERE 子句和 SQL 语句的详细信息,请参阅构建查询表达式使用 Python 指定查询

(默认值为 "")

String
skip_nulls

控制是否跳过使用空值的记录。可以是布尔值(truefalse)、Python 函数或 lambda 表达式。

设置为 True 时,只要记录的属性出现空值(包括几何图形),就跳过该记录。如果设置为 False,skip_nulls 将尝试使用所有记录,而不考虑空值(对于浮点数值,NumPy 支持 nan,对于整数则不支持)。

有关跳过所有包含空值的记录的示例。

import arcpy
array = arcpy.da.TableToNumPyArray(table, fields, skip_nulls=True)

使用 Python 函数或 lambda 表达式可进行更精细的控制,包括对含有空值的所有记录的 OID 值进行记录。在以下两个示例中,使用 lambda 表达式或函数来识别含有空记录的 OID。

使用函数来捕获所有因含空值被跳过的记录。

import arcpy
def getnull(oid):
    nullRows.append(oid)
    return True
nullRows = list()
array = arcpy.da.TableToNumPyArray(table, fields, skip_nulls=getnull)
print(nullRows)

使用 lambda 表达式来捕获所有因含空值被跳过的记录。

import arcpy
nullRows = list()
array = arcpy.da.TableToNumPyArray(table, fields, 
                             skip_nulls=lambda oid: nullRows.append(oid))
print(nullRows)

注注:

在 NumPy 数组中,空值以浮点型(如 nan)和文本类型(如 None)表示。整型不支持空值概念。

(默认值为 False)

Variant
null_value

将输入的空值替换为新值。

在计算 skip_null 之前,替换 null_value

Mask all None's in integer fields with a -9999.

import arcpy
fields = ['field1', 'field2']
arcpy.da.TableToNumPyArray(table, fields, null_value=-9999)

Mask None's in integer fields with different values using a dictionary.

import arcpy
fields = ['field1', 'field2']
nullDict = {'field1':-999999, 'field2':-9999}
arcpy.da.TableToNumPyArray(table, fields, null_value=nullDict)
警告警告:

对于掩膜(例如 -9999)允许将包含空值的整型字段导出到 NumPy 数组的情况,使用这些值进行分析时应格外小心。分析结果可能因引入的值而意外偏斜。

(默认值为 None)

Integer
返回值
数据类型说明
NumPyArray

NumPy 结构化数组。

代码实例

TableToNumPyArray 示例 1

将表转化为 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 示例 2

使用 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])))

相关主题

9/15/2013