Merge and save results after selections using loop Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Merging and exporting results inside for loop using ArcPy?Merge Shapefiles using a loopSelecting rows in a layer using arcpy.SearchCursorLoop using Reclassify function: string 'RemapRange' object is not callableUse results of loop for another raster calculationarcpy: For loop to sequence Processing of two items in list using ArcPy?Stopping arcpy for loop in cases where outputs match wildcard and get fed back to for loop listarcpy for loop to save shapefiles as .layer filesRunning MaxEnt modelling in “dismo” using “loop” and storing the results in seperate objects and foldersMaking loop over shapefile attribute using ArcPy?Multiple selections with a python loop

How to call a function with default parameter through a pointer to function that is the return of another function?

What exactly is a "Meth" in Altered Carbon?

Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?

How to bypass password on Windows XP account?

Apollo command module space walk?

Seeking colloquialism for “just because”

Sci-Fi book where patients in a coma ward all live in a subconscious world linked together

Check which numbers satisfy the condition [A*B*C = A! + B! + C!]

Generate an RGB colour grid

Identify plant with long narrow paired leaves and reddish stems

Identifying polygons that intersect with another layer using QGIS?

English words in a non-english sci-fi novel

Is it true that "carbohydrates are of no use for the basal metabolic need"?

Using et al. for a last / senior author rather than for a first author

Why is my conclusion inconsistent with the van't Hoff equation?

How to align text above triangle figure

Coloring maths inside a tcolorbox

Why aren't air breathing engines used as small first stages

Can a USB port passively 'listen only'?

Single word antonym of "flightless"

What are the pros and cons of Aerospike nosecones?

List *all* the tuples!

What would be the ideal power source for a cybernetic eye?

Why didn't this character "real die" when they blew their stack out in Altered Carbon?



Merge and save results after selections using loop



Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Merging and exporting results inside for loop using ArcPy?Merge Shapefiles using a loopSelecting rows in a layer using arcpy.SearchCursorLoop using Reclassify function: string 'RemapRange' object is not callableUse results of loop for another raster calculationarcpy: For loop to sequence Processing of two items in list using ArcPy?Stopping arcpy for loop in cases where outputs match wildcard and get fed back to for loop listarcpy for loop to save shapefiles as .layer filesRunning MaxEnt modelling in “dismo” using “loop” and storing the results in seperate objects and foldersMaking loop over shapefile attribute using ArcPy?Multiple selections with a python loop



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-1















I want to merge shapefiles after selection inside a loop and then save results to excel files. However, the codes could save the last result to excel files and can not save each merge file.



import arcpy
arcpy.env.workspace = "D:/STAR/GISpro.gdb"
arcpy.env.overwriteOutput = True #overwrite the exsiting files
selection1 = '"gridcode"<=14.3'
selection2 = '"gridcode">14.3 AND "gridcode"<=28.6'
selection3 = '"gridcode">28.6 AND "gridcode"<=42.9'
selection4 = '"gridcode">42.9 AND "gridcode"<=57.2'
selection5 = '"gridcode">57.2 AND "gridcode"<=71.5'
selection6 = '"gridcode">71.5 AND "gridcode"<=85.8'
selection7 = '"gridcode">85.8 AND "gridcode"<=100'

selections = [selection1, selection2, selection3, selection4,
selection5, selection6, selection7]
layers = ["wqi_Conshp_clip27171","wqi_south8shp_clip27171"]

fc = "wqi_north8shp_clip27171"

list = []

rows = arcpy.SearchCursor(fc)
for row in rows:
pop = row.getValue("Shape_Area")
list.append(pop)

Sum_Area27171 = sum(list)

for layer in layers:

arcpy.MakeFeatureLayer_management(layer,layer)

count = 0
statisticstables = []
selectionmerge = layer + "_Merge" # UPDATE THIS

for selection in selections:
count = count + 1
selectionname = layer + "level" + str(count)

statisticstables.append(selectionname)
arcpy.SelectLayerByAttribute_management(layer,'NEW_SELECTION',selection)
arcpy.Statistics_analysis(layer,selectionname,[["Shape_Area","sum"]])
arcpy.AddField_management(selectionname,'rate','DOUBLE')
arcpy.CalculateField_management(selectionname,'rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
print selectionname

try:
arcpy.Merge_management(statisticstables, selectionmerge) #move this line out of the loop.
print selectionmerge
except ExecuteError as e:
print(e)
print(arcpy.GetMessages())

outpath = "D:\STAR\info_data_test_results"
outresult = outpath + "\" + layer + "_Merge" + ".xls"
arcpy.TableToExcel_conversion(selectionmerge,outresult)


As you can see, I use "merge_management" to merge all results from selection loop but could just save the last result as an excel file. Could anyone provide some suggestions about this problem?










share|improve this question
























  • What is selections set to? For tips on writing code snippets see gis.meta.stackexchange.com/questions/4312/…

    – PolyGeo
    Apr 5 at 23:18












  • Does selecationmerge exist already, and is is the same type as the output from the selection (stats table)?

    – smiller
    Apr 7 at 13:39











  • @smiller, thanks for replying. I use "arcpy.env.overwriteOutput = True" to overwrite the existing results. So, maybe "selectionmerge" is not a problem. Thanks again

    – Zhenyu
    Apr 7 at 17:37











  • As is, the layer selection merge is specifically listed in the merge input, so it must exist already during first pass of the loop .Otherwise you can create a list of the intermediate layers and merge only that list after the loop to create each table finishes.

    – smiller
    Apr 7 at 18:31












  • @smiller, thanks for your replying. I am very new to Python. Could you mind giving more specific information about how to do that? Really appreciate your help.

    – Zhenyu
    Apr 7 at 18:39

















-1















I want to merge shapefiles after selection inside a loop and then save results to excel files. However, the codes could save the last result to excel files and can not save each merge file.



import arcpy
arcpy.env.workspace = "D:/STAR/GISpro.gdb"
arcpy.env.overwriteOutput = True #overwrite the exsiting files
selection1 = '"gridcode"<=14.3'
selection2 = '"gridcode">14.3 AND "gridcode"<=28.6'
selection3 = '"gridcode">28.6 AND "gridcode"<=42.9'
selection4 = '"gridcode">42.9 AND "gridcode"<=57.2'
selection5 = '"gridcode">57.2 AND "gridcode"<=71.5'
selection6 = '"gridcode">71.5 AND "gridcode"<=85.8'
selection7 = '"gridcode">85.8 AND "gridcode"<=100'

selections = [selection1, selection2, selection3, selection4,
selection5, selection6, selection7]
layers = ["wqi_Conshp_clip27171","wqi_south8shp_clip27171"]

fc = "wqi_north8shp_clip27171"

list = []

rows = arcpy.SearchCursor(fc)
for row in rows:
pop = row.getValue("Shape_Area")
list.append(pop)

Sum_Area27171 = sum(list)

for layer in layers:

arcpy.MakeFeatureLayer_management(layer,layer)

count = 0
statisticstables = []
selectionmerge = layer + "_Merge" # UPDATE THIS

for selection in selections:
count = count + 1
selectionname = layer + "level" + str(count)

statisticstables.append(selectionname)
arcpy.SelectLayerByAttribute_management(layer,'NEW_SELECTION',selection)
arcpy.Statistics_analysis(layer,selectionname,[["Shape_Area","sum"]])
arcpy.AddField_management(selectionname,'rate','DOUBLE')
arcpy.CalculateField_management(selectionname,'rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
print selectionname

try:
arcpy.Merge_management(statisticstables, selectionmerge) #move this line out of the loop.
print selectionmerge
except ExecuteError as e:
print(e)
print(arcpy.GetMessages())

outpath = "D:\STAR\info_data_test_results"
outresult = outpath + "\" + layer + "_Merge" + ".xls"
arcpy.TableToExcel_conversion(selectionmerge,outresult)


As you can see, I use "merge_management" to merge all results from selection loop but could just save the last result as an excel file. Could anyone provide some suggestions about this problem?










share|improve this question
























  • What is selections set to? For tips on writing code snippets see gis.meta.stackexchange.com/questions/4312/…

    – PolyGeo
    Apr 5 at 23:18












  • Does selecationmerge exist already, and is is the same type as the output from the selection (stats table)?

    – smiller
    Apr 7 at 13:39











  • @smiller, thanks for replying. I use "arcpy.env.overwriteOutput = True" to overwrite the existing results. So, maybe "selectionmerge" is not a problem. Thanks again

    – Zhenyu
    Apr 7 at 17:37











  • As is, the layer selection merge is specifically listed in the merge input, so it must exist already during first pass of the loop .Otherwise you can create a list of the intermediate layers and merge only that list after the loop to create each table finishes.

    – smiller
    Apr 7 at 18:31












  • @smiller, thanks for your replying. I am very new to Python. Could you mind giving more specific information about how to do that? Really appreciate your help.

    – Zhenyu
    Apr 7 at 18:39













-1












-1








-1








I want to merge shapefiles after selection inside a loop and then save results to excel files. However, the codes could save the last result to excel files and can not save each merge file.



import arcpy
arcpy.env.workspace = "D:/STAR/GISpro.gdb"
arcpy.env.overwriteOutput = True #overwrite the exsiting files
selection1 = '"gridcode"<=14.3'
selection2 = '"gridcode">14.3 AND "gridcode"<=28.6'
selection3 = '"gridcode">28.6 AND "gridcode"<=42.9'
selection4 = '"gridcode">42.9 AND "gridcode"<=57.2'
selection5 = '"gridcode">57.2 AND "gridcode"<=71.5'
selection6 = '"gridcode">71.5 AND "gridcode"<=85.8'
selection7 = '"gridcode">85.8 AND "gridcode"<=100'

selections = [selection1, selection2, selection3, selection4,
selection5, selection6, selection7]
layers = ["wqi_Conshp_clip27171","wqi_south8shp_clip27171"]

fc = "wqi_north8shp_clip27171"

list = []

rows = arcpy.SearchCursor(fc)
for row in rows:
pop = row.getValue("Shape_Area")
list.append(pop)

Sum_Area27171 = sum(list)

for layer in layers:

arcpy.MakeFeatureLayer_management(layer,layer)

count = 0
statisticstables = []
selectionmerge = layer + "_Merge" # UPDATE THIS

for selection in selections:
count = count + 1
selectionname = layer + "level" + str(count)

statisticstables.append(selectionname)
arcpy.SelectLayerByAttribute_management(layer,'NEW_SELECTION',selection)
arcpy.Statistics_analysis(layer,selectionname,[["Shape_Area","sum"]])
arcpy.AddField_management(selectionname,'rate','DOUBLE')
arcpy.CalculateField_management(selectionname,'rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
print selectionname

try:
arcpy.Merge_management(statisticstables, selectionmerge) #move this line out of the loop.
print selectionmerge
except ExecuteError as e:
print(e)
print(arcpy.GetMessages())

outpath = "D:\STAR\info_data_test_results"
outresult = outpath + "\" + layer + "_Merge" + ".xls"
arcpy.TableToExcel_conversion(selectionmerge,outresult)


As you can see, I use "merge_management" to merge all results from selection loop but could just save the last result as an excel file. Could anyone provide some suggestions about this problem?










share|improve this question
















I want to merge shapefiles after selection inside a loop and then save results to excel files. However, the codes could save the last result to excel files and can not save each merge file.



import arcpy
arcpy.env.workspace = "D:/STAR/GISpro.gdb"
arcpy.env.overwriteOutput = True #overwrite the exsiting files
selection1 = '"gridcode"<=14.3'
selection2 = '"gridcode">14.3 AND "gridcode"<=28.6'
selection3 = '"gridcode">28.6 AND "gridcode"<=42.9'
selection4 = '"gridcode">42.9 AND "gridcode"<=57.2'
selection5 = '"gridcode">57.2 AND "gridcode"<=71.5'
selection6 = '"gridcode">71.5 AND "gridcode"<=85.8'
selection7 = '"gridcode">85.8 AND "gridcode"<=100'

selections = [selection1, selection2, selection3, selection4,
selection5, selection6, selection7]
layers = ["wqi_Conshp_clip27171","wqi_south8shp_clip27171"]

fc = "wqi_north8shp_clip27171"

list = []

rows = arcpy.SearchCursor(fc)
for row in rows:
pop = row.getValue("Shape_Area")
list.append(pop)

Sum_Area27171 = sum(list)

for layer in layers:

arcpy.MakeFeatureLayer_management(layer,layer)

count = 0
statisticstables = []
selectionmerge = layer + "_Merge" # UPDATE THIS

for selection in selections:
count = count + 1
selectionname = layer + "level" + str(count)

statisticstables.append(selectionname)
arcpy.SelectLayerByAttribute_management(layer,'NEW_SELECTION',selection)
arcpy.Statistics_analysis(layer,selectionname,[["Shape_Area","sum"]])
arcpy.AddField_management(selectionname,'rate','DOUBLE')
arcpy.CalculateField_management(selectionname,'rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
print selectionname

try:
arcpy.Merge_management(statisticstables, selectionmerge) #move this line out of the loop.
print selectionmerge
except ExecuteError as e:
print(e)
print(arcpy.GetMessages())

outpath = "D:\STAR\info_data_test_results"
outresult = outpath + "\" + layer + "_Merge" + ".xls"
arcpy.TableToExcel_conversion(selectionmerge,outresult)


As you can see, I use "merge_management" to merge all results from selection loop but could just save the last result as an excel file. Could anyone provide some suggestions about this problem?







arcpy arcgis-10.0 error-000732 for-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 8 at 23:50







Zhenyu

















asked Apr 5 at 22:25









ZhenyuZhenyu

234




234












  • What is selections set to? For tips on writing code snippets see gis.meta.stackexchange.com/questions/4312/…

    – PolyGeo
    Apr 5 at 23:18












  • Does selecationmerge exist already, and is is the same type as the output from the selection (stats table)?

    – smiller
    Apr 7 at 13:39











  • @smiller, thanks for replying. I use "arcpy.env.overwriteOutput = True" to overwrite the existing results. So, maybe "selectionmerge" is not a problem. Thanks again

    – Zhenyu
    Apr 7 at 17:37











  • As is, the layer selection merge is specifically listed in the merge input, so it must exist already during first pass of the loop .Otherwise you can create a list of the intermediate layers and merge only that list after the loop to create each table finishes.

    – smiller
    Apr 7 at 18:31












  • @smiller, thanks for your replying. I am very new to Python. Could you mind giving more specific information about how to do that? Really appreciate your help.

    – Zhenyu
    Apr 7 at 18:39

















  • What is selections set to? For tips on writing code snippets see gis.meta.stackexchange.com/questions/4312/…

    – PolyGeo
    Apr 5 at 23:18












  • Does selecationmerge exist already, and is is the same type as the output from the selection (stats table)?

    – smiller
    Apr 7 at 13:39











  • @smiller, thanks for replying. I use "arcpy.env.overwriteOutput = True" to overwrite the existing results. So, maybe "selectionmerge" is not a problem. Thanks again

    – Zhenyu
    Apr 7 at 17:37











  • As is, the layer selection merge is specifically listed in the merge input, so it must exist already during first pass of the loop .Otherwise you can create a list of the intermediate layers and merge only that list after the loop to create each table finishes.

    – smiller
    Apr 7 at 18:31












  • @smiller, thanks for your replying. I am very new to Python. Could you mind giving more specific information about how to do that? Really appreciate your help.

    – Zhenyu
    Apr 7 at 18:39
















What is selections set to? For tips on writing code snippets see gis.meta.stackexchange.com/questions/4312/…

– PolyGeo
Apr 5 at 23:18






What is selections set to? For tips on writing code snippets see gis.meta.stackexchange.com/questions/4312/…

– PolyGeo
Apr 5 at 23:18














Does selecationmerge exist already, and is is the same type as the output from the selection (stats table)?

– smiller
Apr 7 at 13:39





Does selecationmerge exist already, and is is the same type as the output from the selection (stats table)?

– smiller
Apr 7 at 13:39













@smiller, thanks for replying. I use "arcpy.env.overwriteOutput = True" to overwrite the existing results. So, maybe "selectionmerge" is not a problem. Thanks again

– Zhenyu
Apr 7 at 17:37





@smiller, thanks for replying. I use "arcpy.env.overwriteOutput = True" to overwrite the existing results. So, maybe "selectionmerge" is not a problem. Thanks again

– Zhenyu
Apr 7 at 17:37













As is, the layer selection merge is specifically listed in the merge input, so it must exist already during first pass of the loop .Otherwise you can create a list of the intermediate layers and merge only that list after the loop to create each table finishes.

– smiller
Apr 7 at 18:31






As is, the layer selection merge is specifically listed in the merge input, so it must exist already during first pass of the loop .Otherwise you can create a list of the intermediate layers and merge only that list after the loop to create each table finishes.

– smiller
Apr 7 at 18:31














@smiller, thanks for your replying. I am very new to Python. Could you mind giving more specific information about how to do that? Really appreciate your help.

– Zhenyu
Apr 7 at 18:39





@smiller, thanks for your replying. I am very new to Python. Could you mind giving more specific information about how to do that? Really appreciate your help.

– Zhenyu
Apr 7 at 18:39










1 Answer
1






active

oldest

votes


















1














This is somewhat guessing since your code snippet doesn't include some of the details, however: it looks like you may be trying to merge a dataset with a dataset that doesn't exist already. The first time you go through the loop, the table you've named "selecationmerge" doesn't exist; in fact it's assigned to an empty string. Additionally it's awkward (and, likely slower) to repeatedly merge.



Two options:



  1. build a list of the tables to merge and then merge all at once

  2. Append the second (and subsequent) tables after the first table is created. Note that this would change the input layer, so if you want to keep the output separate initially it's not the best option.

Modifying your code from the question for the first option (merge after loop is finished):



# set your environment settings if you haven't done so already
arcpy.env.workspace = r"path_to_workspace"
count = 0
statisticstables = []
selectionmerge = r"path_to_output_table" # UPDATE THIS
for selection in selections:
count = count + 1
selectionname = "stats27171wqi_Conlevel" + str(count)
print selectionname
statisticstables.append(selectionname)
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection)
arcpy.Statistics_analysis('wqi_Conshp_clip27171',selectionname,[["Shape_Area","sum"]])
arcpy.AddField_management(selectionname,'rate','DOUBLE')
arcpy.CalculateField_management(selectionname,'rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')

try:
arcpy.Merge_management(statisticstables, selectionmerge) #move this line out of the loop.
print(arcpy.GetMessages())
except ExecuteError as e:
print(e)
print(arcpy.GetMessages())


Additional resources:



Merge: http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/merge.htm#GUID-7D7965A6-8486-4AF1-AAFD-C0168DFD1124



Append: http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/append.htm#C_GUID-FA9BFB50-2B63-4355-929C-E22B915A9103






share|improve this answer























  • thanks for your help. Your answer helps me a lot. I have updated my questions and add another loop outside the original one. Also, I want to save the generated results automatically. @smiller

    – Zhenyu
    Apr 8 at 23:42












  • Please revert your question and add the new question to a different thread. We have a one question per post requirement and this will make it harder for people to find and help you.

    – smiller
    Apr 9 at 2:52











  • sorry about that. I will post a new question. @smiller

    – Zhenyu
    Apr 9 at 20:16












  • Thank you. Don't forget to revert this one, and if a response answered that initial question, remember to mark it as accepted. This will help others who have a similar question find their answers. You can link your new question to this one for reference.

    – smiller
    Apr 9 at 20:38











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "79"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f317951%2fmerge-and-save-results-after-selections-using-loop%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This is somewhat guessing since your code snippet doesn't include some of the details, however: it looks like you may be trying to merge a dataset with a dataset that doesn't exist already. The first time you go through the loop, the table you've named "selecationmerge" doesn't exist; in fact it's assigned to an empty string. Additionally it's awkward (and, likely slower) to repeatedly merge.



Two options:



  1. build a list of the tables to merge and then merge all at once

  2. Append the second (and subsequent) tables after the first table is created. Note that this would change the input layer, so if you want to keep the output separate initially it's not the best option.

Modifying your code from the question for the first option (merge after loop is finished):



# set your environment settings if you haven't done so already
arcpy.env.workspace = r"path_to_workspace"
count = 0
statisticstables = []
selectionmerge = r"path_to_output_table" # UPDATE THIS
for selection in selections:
count = count + 1
selectionname = "stats27171wqi_Conlevel" + str(count)
print selectionname
statisticstables.append(selectionname)
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection)
arcpy.Statistics_analysis('wqi_Conshp_clip27171',selectionname,[["Shape_Area","sum"]])
arcpy.AddField_management(selectionname,'rate','DOUBLE')
arcpy.CalculateField_management(selectionname,'rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')

try:
arcpy.Merge_management(statisticstables, selectionmerge) #move this line out of the loop.
print(arcpy.GetMessages())
except ExecuteError as e:
print(e)
print(arcpy.GetMessages())


Additional resources:



Merge: http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/merge.htm#GUID-7D7965A6-8486-4AF1-AAFD-C0168DFD1124



Append: http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/append.htm#C_GUID-FA9BFB50-2B63-4355-929C-E22B915A9103






share|improve this answer























  • thanks for your help. Your answer helps me a lot. I have updated my questions and add another loop outside the original one. Also, I want to save the generated results automatically. @smiller

    – Zhenyu
    Apr 8 at 23:42












  • Please revert your question and add the new question to a different thread. We have a one question per post requirement and this will make it harder for people to find and help you.

    – smiller
    Apr 9 at 2:52











  • sorry about that. I will post a new question. @smiller

    – Zhenyu
    Apr 9 at 20:16












  • Thank you. Don't forget to revert this one, and if a response answered that initial question, remember to mark it as accepted. This will help others who have a similar question find their answers. You can link your new question to this one for reference.

    – smiller
    Apr 9 at 20:38















1














This is somewhat guessing since your code snippet doesn't include some of the details, however: it looks like you may be trying to merge a dataset with a dataset that doesn't exist already. The first time you go through the loop, the table you've named "selecationmerge" doesn't exist; in fact it's assigned to an empty string. Additionally it's awkward (and, likely slower) to repeatedly merge.



Two options:



  1. build a list of the tables to merge and then merge all at once

  2. Append the second (and subsequent) tables after the first table is created. Note that this would change the input layer, so if you want to keep the output separate initially it's not the best option.

Modifying your code from the question for the first option (merge after loop is finished):



# set your environment settings if you haven't done so already
arcpy.env.workspace = r"path_to_workspace"
count = 0
statisticstables = []
selectionmerge = r"path_to_output_table" # UPDATE THIS
for selection in selections:
count = count + 1
selectionname = "stats27171wqi_Conlevel" + str(count)
print selectionname
statisticstables.append(selectionname)
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection)
arcpy.Statistics_analysis('wqi_Conshp_clip27171',selectionname,[["Shape_Area","sum"]])
arcpy.AddField_management(selectionname,'rate','DOUBLE')
arcpy.CalculateField_management(selectionname,'rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')

try:
arcpy.Merge_management(statisticstables, selectionmerge) #move this line out of the loop.
print(arcpy.GetMessages())
except ExecuteError as e:
print(e)
print(arcpy.GetMessages())


Additional resources:



Merge: http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/merge.htm#GUID-7D7965A6-8486-4AF1-AAFD-C0168DFD1124



Append: http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/append.htm#C_GUID-FA9BFB50-2B63-4355-929C-E22B915A9103






share|improve this answer























  • thanks for your help. Your answer helps me a lot. I have updated my questions and add another loop outside the original one. Also, I want to save the generated results automatically. @smiller

    – Zhenyu
    Apr 8 at 23:42












  • Please revert your question and add the new question to a different thread. We have a one question per post requirement and this will make it harder for people to find and help you.

    – smiller
    Apr 9 at 2:52











  • sorry about that. I will post a new question. @smiller

    – Zhenyu
    Apr 9 at 20:16












  • Thank you. Don't forget to revert this one, and if a response answered that initial question, remember to mark it as accepted. This will help others who have a similar question find their answers. You can link your new question to this one for reference.

    – smiller
    Apr 9 at 20:38













1












1








1







This is somewhat guessing since your code snippet doesn't include some of the details, however: it looks like you may be trying to merge a dataset with a dataset that doesn't exist already. The first time you go through the loop, the table you've named "selecationmerge" doesn't exist; in fact it's assigned to an empty string. Additionally it's awkward (and, likely slower) to repeatedly merge.



Two options:



  1. build a list of the tables to merge and then merge all at once

  2. Append the second (and subsequent) tables after the first table is created. Note that this would change the input layer, so if you want to keep the output separate initially it's not the best option.

Modifying your code from the question for the first option (merge after loop is finished):



# set your environment settings if you haven't done so already
arcpy.env.workspace = r"path_to_workspace"
count = 0
statisticstables = []
selectionmerge = r"path_to_output_table" # UPDATE THIS
for selection in selections:
count = count + 1
selectionname = "stats27171wqi_Conlevel" + str(count)
print selectionname
statisticstables.append(selectionname)
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection)
arcpy.Statistics_analysis('wqi_Conshp_clip27171',selectionname,[["Shape_Area","sum"]])
arcpy.AddField_management(selectionname,'rate','DOUBLE')
arcpy.CalculateField_management(selectionname,'rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')

try:
arcpy.Merge_management(statisticstables, selectionmerge) #move this line out of the loop.
print(arcpy.GetMessages())
except ExecuteError as e:
print(e)
print(arcpy.GetMessages())


Additional resources:



Merge: http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/merge.htm#GUID-7D7965A6-8486-4AF1-AAFD-C0168DFD1124



Append: http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/append.htm#C_GUID-FA9BFB50-2B63-4355-929C-E22B915A9103






share|improve this answer













This is somewhat guessing since your code snippet doesn't include some of the details, however: it looks like you may be trying to merge a dataset with a dataset that doesn't exist already. The first time you go through the loop, the table you've named "selecationmerge" doesn't exist; in fact it's assigned to an empty string. Additionally it's awkward (and, likely slower) to repeatedly merge.



Two options:



  1. build a list of the tables to merge and then merge all at once

  2. Append the second (and subsequent) tables after the first table is created. Note that this would change the input layer, so if you want to keep the output separate initially it's not the best option.

Modifying your code from the question for the first option (merge after loop is finished):



# set your environment settings if you haven't done so already
arcpy.env.workspace = r"path_to_workspace"
count = 0
statisticstables = []
selectionmerge = r"path_to_output_table" # UPDATE THIS
for selection in selections:
count = count + 1
selectionname = "stats27171wqi_Conlevel" + str(count)
print selectionname
statisticstables.append(selectionname)
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection)
arcpy.Statistics_analysis('wqi_Conshp_clip27171',selectionname,[["Shape_Area","sum"]])
arcpy.AddField_management(selectionname,'rate','DOUBLE')
arcpy.CalculateField_management(selectionname,'rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')

try:
arcpy.Merge_management(statisticstables, selectionmerge) #move this line out of the loop.
print(arcpy.GetMessages())
except ExecuteError as e:
print(e)
print(arcpy.GetMessages())


Additional resources:



Merge: http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/merge.htm#GUID-7D7965A6-8486-4AF1-AAFD-C0168DFD1124



Append: http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/append.htm#C_GUID-FA9BFB50-2B63-4355-929C-E22B915A9103







share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 8 at 4:21









smillersmiller

2,319217




2,319217












  • thanks for your help. Your answer helps me a lot. I have updated my questions and add another loop outside the original one. Also, I want to save the generated results automatically. @smiller

    – Zhenyu
    Apr 8 at 23:42












  • Please revert your question and add the new question to a different thread. We have a one question per post requirement and this will make it harder for people to find and help you.

    – smiller
    Apr 9 at 2:52











  • sorry about that. I will post a new question. @smiller

    – Zhenyu
    Apr 9 at 20:16












  • Thank you. Don't forget to revert this one, and if a response answered that initial question, remember to mark it as accepted. This will help others who have a similar question find their answers. You can link your new question to this one for reference.

    – smiller
    Apr 9 at 20:38

















  • thanks for your help. Your answer helps me a lot. I have updated my questions and add another loop outside the original one. Also, I want to save the generated results automatically. @smiller

    – Zhenyu
    Apr 8 at 23:42












  • Please revert your question and add the new question to a different thread. We have a one question per post requirement and this will make it harder for people to find and help you.

    – smiller
    Apr 9 at 2:52











  • sorry about that. I will post a new question. @smiller

    – Zhenyu
    Apr 9 at 20:16












  • Thank you. Don't forget to revert this one, and if a response answered that initial question, remember to mark it as accepted. This will help others who have a similar question find their answers. You can link your new question to this one for reference.

    – smiller
    Apr 9 at 20:38
















thanks for your help. Your answer helps me a lot. I have updated my questions and add another loop outside the original one. Also, I want to save the generated results automatically. @smiller

– Zhenyu
Apr 8 at 23:42






thanks for your help. Your answer helps me a lot. I have updated my questions and add another loop outside the original one. Also, I want to save the generated results automatically. @smiller

– Zhenyu
Apr 8 at 23:42














Please revert your question and add the new question to a different thread. We have a one question per post requirement and this will make it harder for people to find and help you.

– smiller
Apr 9 at 2:52





Please revert your question and add the new question to a different thread. We have a one question per post requirement and this will make it harder for people to find and help you.

– smiller
Apr 9 at 2:52













sorry about that. I will post a new question. @smiller

– Zhenyu
Apr 9 at 20:16






sorry about that. I will post a new question. @smiller

– Zhenyu
Apr 9 at 20:16














Thank you. Don't forget to revert this one, and if a response answered that initial question, remember to mark it as accepted. This will help others who have a similar question find their answers. You can link your new question to this one for reference.

– smiller
Apr 9 at 20:38





Thank you. Don't forget to revert this one, and if a response answered that initial question, remember to mark it as accepted. This will help others who have a similar question find their answers. You can link your new question to this one for reference.

– smiller
Apr 9 at 20:38

















draft saved

draft discarded
















































Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f317951%2fmerge-and-save-results-after-selections-using-loop%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

រឿង រ៉ូមេអូ និង ហ្ស៊ុយលីយេ សង្ខេបរឿង តួអង្គ បញ្ជីណែនាំ

Crop image to path created in TikZ? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Crop an inserted image?TikZ pictures does not appear in posterImage behind and beyond crop marks?Tikz picture as large as possible on A4 PageTransparency vs image compression dilemmaHow to crop background from image automatically?Image does not cropTikzexternal capturing crop marks when externalizing pgfplots?How to include image path that contains a dollar signCrop image with left size given

Romeo and Juliet ContentsCharactersSynopsisSourcesDate and textThemes and motifsCriticism and interpretationLegacyScene by sceneSee alsoNotes and referencesSourcesExternal linksNavigation menu"Consumer Price Index (estimate) 1800–"10.2307/28710160037-3222287101610.1093/res/II.5.31910.2307/45967845967810.2307/2869925286992510.1525/jams.1982.35.3.03a00050"Dada Masilo: South African dancer who breaks the rules"10.1093/res/os-XV.57.1610.2307/28680942868094"Sweet Sorrow: Mann-Korman's Romeo and Juliet Closes Sept. 5 at MN's Ordway"the original10.2307/45957745957710.1017/CCOL0521570476.009"Ram Leela box office collections hit massive Rs 100 crore, pulverises prediction"Archived"Broadway Revival of Romeo and Juliet, Starring Orlando Bloom and Condola Rashad, Will Close Dec. 8"Archived10.1075/jhp.7.1.04hon"Wherefore art thou, Romeo? To make us laugh at Navy Pier"the original10.1093/gmo/9781561592630.article.O006772"Ram-leela Review Roundup: Critics Hail Film as Best Adaptation of Romeo and Juliet"Archived10.2307/31946310047-77293194631"Romeo and Juliet get Twitter treatment""Juliet's Nurse by Lois Leveen""Romeo and Juliet: Orlando Bloom's Broadway Debut Released in Theaters for Valentine's Day"Archived"Romeo and Juliet Has No Balcony"10.1093/gmo/9781561592630.article.O00778110.2307/2867423286742310.1076/enst.82.2.115.959510.1080/00138380601042675"A plague o' both your houses: error in GCSE exam paper forces apology""Juliet of the Five O'Clock Shadow, and Other Wonders"10.2307/33912430027-4321339124310.2307/28487440038-7134284874410.2307/29123140149-661129123144728341M"Weekender Guide: Shakespeare on The Drive""balcony"UK public library membership"romeo"UK public library membership10.1017/CCOL9780521844291"Post-Zionist Critique on Israel and the Palestinians Part III: Popular Culture"10.2307/25379071533-86140377-919X2537907"Capulets and Montagues: UK exam board admit mixing names up in Romeo and Juliet paper"Istoria Novellamente Ritrovata di Due Nobili Amanti2027/mdp.390150822329610820-750X"GCSE exam error: Board accidentally rewrites Shakespeare"10.2307/29176390149-66112917639"Exam board apologises after error in English GCSE paper which confused characters in Shakespeare's Romeo and Juliet""From Mariotto and Ganozza to Romeo and Guilietta: Metamorphoses of a Renaissance Tale"10.2307/37323537323510.2307/2867455286745510.2307/28678912867891"10 Questions for Taylor Swift"10.2307/28680922868092"Haymarket Theatre""The Zeffirelli Way: Revealing Talk by Florentine Director""Michael Smuin: 1938-2007 / Prolific dance director had showy career"The Life and Art of Edwin BoothRomeo and JulietRomeo and JulietRomeo and JulietRomeo and JulietEasy Read Romeo and JulietRomeo and Julieteeecb12003684p(data)4099369-3n8211610759dbe00d-a9e2-41a3-b2c1-977dd692899302814385X313670221313670221