<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Using Python to unpivot a table. in Data Management Questions</title>
    <link>https://community.esri.com/t5/data-management-questions/using-python-to-unpivot-a-table/m-p/1090280#M43517</link>
    <description>&lt;LI-CODE lang="python"&gt;# your input table and fields
in_fc = "..."
in_fields = ["OBJECTID", "RELID", "DATE", "AUTHORS"]

# your output table and fields
# this can be in_fc and in_fields, but for testing I suggest you create a new table...
# if you really need to copy ObjectID, you have to create a new field, FID in my example
out_fc = "..."
out_fields = ["FID", "RELID", "DATE", "AUTHORS"]

# index of the field that has to be split
# !python uses zero-based indexing!
split_field_index = 3
split_pattern = ", "


# read data
data = [list(row) for row in arcpy.da.SearchCursor(in_fc, in_fields)]

# optional: delete all rows from out_fc
# !!! if out_fc is the origin table of a relationship class, the foreign key in the related table will be set to Null !!!
arcpy.management.TruncateTable(out_fc)

with arcpy.da.InsertCursor(out_fc, out_fields) as cursor:
    # loop through original data
    for row in data:
        split_field = row[split_field_index]
        # split_field is empty? just copy the row
        if split_field is None:
            cursor.insertRow(row)
            continue
        # split split_field 
        split_values = split_field.split(split_pattern)
        # loop through split_values and insert rows
        for value in split_values:
            row[split_field_index] = value
            cursor.insertRow(row)&lt;/LI-CODE&gt;</description>
    <pubDate>Thu, 19 Aug 2021 09:09:10 GMT</pubDate>
    <dc:creator>JohannesLindner</dc:creator>
    <dc:date>2021-08-19T09:09:10Z</dc:date>
    <item>
      <title>Using Python to unpivot a table.</title>
      <link>https://community.esri.com/t5/data-management-questions/using-python-to-unpivot-a-table/m-p/1090205#M43514</link>
      <description>&lt;P&gt;I have a table in which one of the fields is currently filled with a comma separated list. (Made-up sample below)&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;OBJECTID&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;RELID&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;DATE&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;Authors&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;1&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;3&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;7/04/1996&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;T. Pratchett, N. Gaiman&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to split this so that I have a record per each author, with the rest of the information maintained.&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;OBJECTID&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;RELID&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;DATE&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;Authors&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;1&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;3&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;7/04/1995&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;T. Pratchett&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;1&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;3&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;7/04/1995&lt;/TD&gt;&lt;TD width="25%"&gt;N. Gaiman&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The workflow, I think, would be&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Get the table&lt;/LI&gt;&lt;LI&gt;Search the table by row&lt;/LI&gt;&lt;LI&gt;Split the Authors column into a list&lt;/LI&gt;&lt;LI&gt;If there is only 1 or 0 authors, ignore it and move on&lt;/LI&gt;&lt;LI&gt;If there are two or more authors,&lt;UL&gt;&lt;LI&gt;For each author, create a new row and pass the original values from the row it came from&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;I'm very much a beginner at this, so the following is my best guess before I ran out of ideas, specifically on how to get the original values to the new rows.&amp;nbsp;&lt;/P&gt;&lt;P&gt;If anyone could give me some pointers, I'd really appreciate it.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy, os
from collections import defaultdict

inFC = #######

myFeatures = dict()

with arcpy.da.SearchCursor(inFC, ['OBJECTID', 'RELID', 'DATE', 'AUTHORS']) as cursor:
    for row in cursor:
        listA= list(row[4].split(","))
        if count(listA)== 1:
            ""
        elif count(listA) == 0:
            ""
        else:
            rows = arcp.da.InsertCursor(inFC)
            for x in listA:
                row = rows.NewRow()
                row.setValue("RELID", &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Aug 2021 00:04:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/data-management-questions/using-python-to-unpivot-a-table/m-p/1090205#M43514</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2021-08-19T00:04:53Z</dc:date>
    </item>
    <item>
      <title>Re: Using Python to unpivot a table.</title>
      <link>https://community.esri.com/t5/data-management-questions/using-python-to-unpivot-a-table/m-p/1090230#M43515</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I would just make a new table. logic without the esri stuff could look like this:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;rows = [[&lt;SPAN&gt;'1'&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;'3'&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;'7/04/1996'&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;'T. Pratchett, N. Gaiman'&lt;/SPAN&gt;]&lt;SPAN&gt;,&lt;BR /&gt;&lt;/SPAN&gt;        [&lt;SPAN&gt;'2'&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;'4'&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;'8/04/1996'&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;'person a'&lt;/SPAN&gt;]&lt;SPAN&gt;,&lt;BR /&gt;&lt;/SPAN&gt;        [&lt;SPAN&gt;'3'&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;'1'&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;'9/04/1996'&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;'person a, person b, person c'&lt;/SPAN&gt;]]&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;for &lt;/SPAN&gt;row &lt;SPAN&gt;in &lt;/SPAN&gt;rows:&lt;BR /&gt;    &lt;SPAN&gt;for &lt;/SPAN&gt;author &lt;SPAN&gt;in &lt;/SPAN&gt;[x.strip() &lt;SPAN&gt;for &lt;/SPAN&gt;x &lt;SPAN&gt;in &lt;/SPAN&gt;row[-&lt;SPAN&gt;1&lt;/SPAN&gt;].split(&lt;SPAN&gt;','&lt;/SPAN&gt;)]:&lt;BR /&gt;        &lt;SPAN&gt;print &lt;/SPAN&gt;row[&lt;SPAN&gt;1&lt;/SPAN&gt;:-&lt;SPAN&gt;1&lt;/SPAN&gt;] + [author]&lt;/PRE&gt;&lt;P&gt;['3', '7/04/1996', 'T. Pratchett']&lt;BR /&gt;['3', '7/04/1996', 'N. Gaiman']&lt;BR /&gt;['4', '8/04/1996', 'person a']&lt;BR /&gt;['1', '9/04/1996', 'person a']&lt;BR /&gt;['1', '9/04/1996', 'person b']&lt;BR /&gt;['1', '9/04/1996', 'person c']&lt;/P&gt;</description>
      <pubDate>Thu, 19 Aug 2021 01:00:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/data-management-questions/using-python-to-unpivot-a-table/m-p/1090230#M43515</guid>
      <dc:creator>forestknutsen1</dc:creator>
      <dc:date>2021-08-19T01:00:39Z</dc:date>
    </item>
    <item>
      <title>Re: Using Python to unpivot a table.</title>
      <link>https://community.esri.com/t5/data-management-questions/using-python-to-unpivot-a-table/m-p/1090280#M43517</link>
      <description>&lt;LI-CODE lang="python"&gt;# your input table and fields
in_fc = "..."
in_fields = ["OBJECTID", "RELID", "DATE", "AUTHORS"]

# your output table and fields
# this can be in_fc and in_fields, but for testing I suggest you create a new table...
# if you really need to copy ObjectID, you have to create a new field, FID in my example
out_fc = "..."
out_fields = ["FID", "RELID", "DATE", "AUTHORS"]

# index of the field that has to be split
# !python uses zero-based indexing!
split_field_index = 3
split_pattern = ", "


# read data
data = [list(row) for row in arcpy.da.SearchCursor(in_fc, in_fields)]

# optional: delete all rows from out_fc
# !!! if out_fc is the origin table of a relationship class, the foreign key in the related table will be set to Null !!!
arcpy.management.TruncateTable(out_fc)

with arcpy.da.InsertCursor(out_fc, out_fields) as cursor:
    # loop through original data
    for row in data:
        split_field = row[split_field_index]
        # split_field is empty? just copy the row
        if split_field is None:
            cursor.insertRow(row)
            continue
        # split split_field 
        split_values = split_field.split(split_pattern)
        # loop through split_values and insert rows
        for value in split_values:
            row[split_field_index] = value
            cursor.insertRow(row)&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 19 Aug 2021 09:09:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/data-management-questions/using-python-to-unpivot-a-table/m-p/1090280#M43517</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2021-08-19T09:09:10Z</dc:date>
    </item>
    <item>
      <title>Re: Using Python to unpivot a table.</title>
      <link>https://community.esri.com/t5/data-management-questions/using-python-to-unpivot-a-table/m-p/1090594#M43522</link>
      <description>&lt;P&gt;This works great!&lt;/P&gt;&lt;P&gt;Thank you for your help again.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Aug 2021 20:36:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/data-management-questions/using-python-to-unpivot-a-table/m-p/1090594#M43522</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2021-08-19T20:36:08Z</dc:date>
    </item>
  </channel>
</rss>

