ArcGIS for Desktop

  • 文档
  • 合约
  • 支持

  • My Profile
  • 帮助
  • Sign Out
ArcGIS for Desktop

ArcGIS Online

专为贵组织打造的制图平台

ArcGIS for Desktop

全面的专业性 GIS

ArcGIS for Server

面向企业的 GIS

ArcGIS for Developers

用于构建位置感知应用程序的工具

ArcGIS Solutions

适用于行业的免费模板地图和应用程序

ArcGIS Marketplace

获取适用于组织的应用程序和数据

  • 文档
  • 合约
  • 支持
Esri
  • 登录
user
  • 我的个人资料
  • 登出

帮助

  • 主页
  • 入门
  • 制图
  • 分析
  • 管理数据
  • 工具
  • 更多...

TableToNumPyArray

  • 摘要
  • 讨论
  • 语法
  • 代码实例

摘要

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

讨论

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

要将要素类转换成 NumPy 数组,请改为使用 FeatureClassToNumPyArray 函数。

语法

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 语句的详细信息,请参阅构建查询表达式。

(默认值为 "")

String
skip_nulls

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

设置为 True 时,只要记录的属性是空值(包括几何图形),就跳过该记录。如果设置为 False,skip_nulls 将尝试使用所有记录,而不考虑空值。在 NumPy 数组中,对于浮点数值,空值表示为 nan(not a number,不是数字),但不适用于整数。

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

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_nulls 之前,替换 null_value。

Mask None values in integer fields with a -9999.

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

Mask None values 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])))

相关主题

  • NumPyArrayToFeatureClass
  • NumPyArrayToTable
  • FeatureClassToNumPyArray
  • 在 ArcGIS 中使用 NumPy
  • ExtendTable
有关此主题的反馈?

ArcGIS for Desktop

  • 主页
  • 文档
  • 合约
  • 支持

ArcGIS 平台

  • ArcGIS Online
  • ArcGIS for Desktop
  • ArcGIS for Server
  • ArcGIS for Developers
  • ArcGIS Solutions
  • ArcGIS Marketplace

关于 Esri

  • 关于我们
  • 招贤纳士
  • 内部人员博客
  • 用户大会
  • 开发者峰会
Esri
© Copyright 2016 Environmental Systems Research Institute, Inc. | 隐私政策 | 法律声明