I am seeking a method to merge table outputs from ZonalHistogram (ZH) using a Python script. These files have a simple two-column format. E.g., AAA.dbf has two fields:
- LABEL - the X-axis with values ranging from 0 to 100
- AAA - the Y-axis counts produced by ZH
Similarly, BBB.dbf has two fields, LABEL and BBB, CCC.dbf has LABEL and CCC. I am able to plot these individual table files with arcpy graph, or render them in Excel.
I am trying to merge these files into a single output with the structure:
LABEL AAA BBB CCC
0 - 100 ZH values ZH values ZH values
I started simply with:
tablesList <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListTables<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Merge_management<SPAN class="punctuation token">(</SPAN>tablesList<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"merged.dbf"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN></SPAN>
which gave me the four columns I was after but the output was arranged as:
rows 0 - 100: LABEL (0 - 100) ZH values 0 0
rows 101 - 201: LABEL (0 - 100) 0 ZH values 0
rows 202 - 302: Label (0 - 100) 0 0 ZH values
I have attempted to use field mapping as follows:
<SPAN class="keyword token">import</SPAN> arcpy
TESTDIR <SPAN class="operator token">=</SPAN> <SPAN class="operator token"><</SPAN>some dir<SPAN class="operator token">></SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> TESTDIR
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>scratchWorkspace <SPAN class="operator token">=</SPAN> TESTDIR
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
tablesList <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListTables<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
inFields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>u<SPAN class="string token">'LABEL'</SPAN><SPAN class="punctuation token">,</SPAN> u<SPAN class="string token">'AAA'</SPAN><SPAN class="punctuation token">,</SPAN> u<SPAN class="string token">'BBB'</SPAN><SPAN class="punctuation token">,</SPAN> u<SPAN class="string token">'CCC'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># Generated by arcpy.Describe()</SPAN>
fieldMappings <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>FieldMappings<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Deal with "LABEL" as special case (first field of first table)</SPAN>
fMap <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>FieldMap<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
fMap<SPAN class="punctuation token">.</SPAN>addInputField<SPAN class="punctuation token">(</SPAN>tablesList<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> inFields<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
outField <SPAN class="operator token">=</SPAN> fMap<SPAN class="punctuation token">.</SPAN>outputField
outField<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">=</SPAN> inFields<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
fMap<SPAN class="punctuation token">.</SPAN>outputField <SPAN class="operator token">=</SPAN> outField
fieldMappings<SPAN class="punctuation token">.</SPAN>addFieldMap<SPAN class="punctuation token">(</SPAN>fMap<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Add the second field of each file to the map</SPAN>
<SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> range<SPAN class="punctuation token">(</SPAN>len<SPAN class="punctuation token">(</SPAN>tablesList<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
fMap <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>FieldMap<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
fMap<SPAN class="punctuation token">.</SPAN>addInputField<SPAN class="punctuation token">(</SPAN>tablesList<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> inFields<SPAN class="punctuation token">[</SPAN>i <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
outField <SPAN class="operator token">=</SPAN> fMap<SPAN class="punctuation token">.</SPAN>outputField
outField<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">=</SPAN> inFields<SPAN class="punctuation token">[</SPAN>i <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN>
fMap<SPAN class="punctuation token">.</SPAN>outputField <SPAN class="operator token">=</SPAN> outField
fieldMappings<SPAN class="punctuation token">.</SPAN>addFieldMap<SPAN class="punctuation token">(</SPAN>fMap<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Merge_management<SPAN class="punctuation token">(</SPAN>tablesList<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"merged.dbf"</SPAN><SPAN class="punctuation token">,</SPAN> fieldMappings<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
The output was the same. It seems I have merely replicated the built-in Merge_management. I would be most grateful for any advice on modifying the script to generate the merged output I am after.