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

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

QGIS export composer to PDF scale the map [closed] Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Print Composer QGIS 2.6, how to export image?QGIS 2.8.1 print composer won't export all OpenCycleMap base layer tilesSave Print/Map QGIS composer view as PNG/PDF using Python (without changing anything in visible layout)?Export QGIS Print Composer PDF with searchable text labelsQGIS Print Composer does not change from landscape to portrait orientation?How can I avoid map size and scale changes in print composer?Fuzzy PDF export in QGIS running on macSierra OSExport the legend into its 100% size using Print ComposerScale-dependent rendering in QGIS PDF output

PDF-ში გადმოწერა სანავიგაციო მენიუproject page