【问题标题】:How to split a row with multiple sets of data in different columns into mutiple rows如何将具有不同列中多组数据的行拆分为多行
【发布时间】:2021-11-16 17:03:23
【问题描述】:

大家!我对从该字段收集的数据有疑问,其中每一行在不同的列中有多组数据(参见输入)。我试图将每组数据分成一行(见预期输出)。

输入:

第 1 行:ID A1 B1 C1 D1 A2 B2 C2 D2 A3 B3 C3 D3 A4 B4 C4 D4 名称1 名称2 名称3 名称4 日期

预期输出:

第 1 行:ID A1 B1 C1 D1 名称1 日期

第 2 行:ID A2 B2 C2 D2 名称2 日期

第 3 行:ID A3 B3 C3 D3 名称3 日期

第 4 行:ID A4 B4 C4 D4 名称4 日期

我发现有一个类似的post,但它使用的是 VBA 脚本。我是一名 GIS 人员,对 python 的 ArcPy 站点包有一些经验。我在使用 ArcPy 构建逻辑工作流时遇到了一些麻烦。我想知道是否有人遇到过类似的情况,并且知道这是否可以使用 Python 和 ArcPy 或任何其他站点包来完成。如果是这样,您能否提供一些关于我应该研究哪些站点包和工具的提示?

【问题讨论】:

    标签: python excel arcpy


    【解决方案1】:

    使用arcpy 执行此操作的方法是使用光标。 创建一个新表,遍历您的数据,拆分它们并在新表中插入每一行。

    这是使用来自 ArcMap(不是 ArcGIS Pro)的arcpy

    import arcpy
    import os
    
    src_data = ''  # Path to your data
    dst_data = ''  # New table that will be create. 
    arcpy.CreateTable_management(os.path.dirname(dst_data),
                                 os.path.basename(dst_data))
    
    # Create the correct fields in the new table
    # Assuming the fields have the same name as your data in Row1
    # Also assuming the A1, A2, A3 fields .... have the same type/scale/precision/length/etc
    # Index split table (index from the Row1 you gave in your example)
    dst_idx_split = [[0, 1, 5, 9, 13, 17, 21],  # ID A1 B1 C1 D1 Name1 Date
                     [0, 2, 6, 10, 14, 18, 21], # ID A2 B2 C2 D2 Name2 Date
                     [0, 3, 7, 11, 15, 19, 21], # etc.
                     [0, 4, 8, 12, 16, 20, 21]]
    
    src_fields = arcpy.ListFields(src_data)
    for idx in dst_idx_split[0]:
        field = src_fields[idx]
        field_name = field.baseName if field_name in ['ID', 'Data'] else field_name[:-1]  # remove the 1/2/3/4 in the field name
        arcpy.AddField_management(dst_data, field_name, field.type, field.precision, 
                                  field.scale, field.length, field.alias, 
                                  field.isNullable, field.required, field.domain)
    
    # Copy the data
    i_cur = arcpy.da.InsertCursor(dst_data, '*')
    
    with arcpy.da.SearchCursor(src_data, '*') as s_cur:
        for row in s_cur:  # for each row of your source data
            for idxs in dst_idx_split: 
                dst_row = [row[idx] for idx in idxs] # extract the data for the new line 
                i_cur.insertRow(dst_row)  # insert it in new table
    del i_cur
    

    【讨论】:

    • 非常感谢!这非常有帮助!我想知道在 ArcGIS Pro 中是否有任何无法使用的功能?我习惯使用 Python 3.0x 在 Pro 中工作?
    • 某些函数的名称已随 ArcGIS Pro 更改,但一旦替换,代码应该可以工作:) 就像 arcpy.CreateTable_management 变为 arcpy.management.CreateTable 但两个函数的参数相同。 arcpy.AddField_management -> arcpy.management.AddField。其余的(光标和 ListFields)是一样的
    • 再次非常感谢您!其实pro都接受,第一个是CreateTable_management比较流行,教材和ArcGIS帮助使用示例代码作为第一个。
    • 再次非常感谢您!我已经完成了代码。效果很好。
    • 如果您的问题已结束,请投票并验证我的回答:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    相关资源
    最近更新 更多