Merge and save results after selections using loop The 2019 Stack Overflow Developer Survey Results Are InMerging 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
The phrase "to the numbers born"?
How to support a colleague who finds meetings extremely tiring?
What is the grammatical structure of "Il est de formation classique"?
How can I add encounters in the Lost Mine of Phandelver campaign without giving PCs too much XP?
What is the meaning of Triage in Cybersec world?
Why doesn't UInt have a toDouble()?
Is it safe to harvest rainwater that fell on solar panels?
Deal with toxic manager when you can't quit
Table Fragmentation
Loose spokes after only a few rides
Is bread bad for ducks?
writing variables above the numbers in tikz picture
"as much details as you can remember"
Output the Arecibo Message
Keeping a retro style to sci-fi spaceships?
Relationship between Gromov-Witten and Taubes' Gromov invariant
! Package inputenc Error: Unicode character (U+2061) (inputenc) not set up for use with LaTeX
Are there any other methods to apply to solving simultaneous equations?
Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?
Did any laptop computers have a built-in 5 1/4 inch floppy drive?
How do I free up internal storage if I don't have any apps downloaded?
Correct punctuation for showing a character's confusion
Why doesn't mkfifo with a mode of 1755 grant read permissions and sticky bit to the user?
Why not take a picture of a closer black hole?
Merge and save results after selections using loop
The 2019 Stack Overflow Developer Survey Results Are InMerging 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;
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
|
show 1 more comment
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
What isselections
set to? For tips on writing code snippets see gis.meta.stackexchange.com/questions/4312/…
– PolyGeo♦
Apr 5 at 23:18
Doesselecationmerge
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
|
show 1 more comment
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
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
arcpy arcgis-10.0 error-000732 for-loop
edited 2 days ago
Zhenyu
asked Apr 5 at 22:25
ZhenyuZhenyu
184
184
What isselections
set to? For tips on writing code snippets see gis.meta.stackexchange.com/questions/4312/…
– PolyGeo♦
Apr 5 at 23:18
Doesselecationmerge
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
|
show 1 more comment
What isselections
set to? For tips on writing code snippets see gis.meta.stackexchange.com/questions/4312/…
– PolyGeo♦
Apr 5 at 23:18
Doesselecationmerge
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
|
show 1 more comment
1 Answer
1
active
oldest
votes
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:
- build a list of the tables to merge and then merge all at once
- 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
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
2 days ago
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
2 days ago
sorry about that. I will post a new question. @smiller
– Zhenyu
2 days ago
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
2 days ago
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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:
- build a list of the tables to merge and then merge all at once
- 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
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
2 days ago
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
2 days ago
sorry about that. I will post a new question. @smiller
– Zhenyu
2 days ago
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
2 days ago
add a comment |
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:
- build a list of the tables to merge and then merge all at once
- 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
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
2 days ago
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
2 days ago
sorry about that. I will post a new question. @smiller
– Zhenyu
2 days ago
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
2 days ago
add a comment |
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:
- build a list of the tables to merge and then merge all at once
- 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
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:
- build a list of the tables to merge and then merge all at once
- 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
answered Apr 8 at 4:21
smillersmiller
2,309217
2,309217
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
2 days ago
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
2 days ago
sorry about that. I will post a new question. @smiller
– Zhenyu
2 days ago
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
2 days ago
add a comment |
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
2 days ago
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
2 days ago
sorry about that. I will post a new question. @smiller
– Zhenyu
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
sorry about that. I will post a new question. @smiller
– Zhenyu
2 days ago
sorry about that. I will post a new question. @smiller
– Zhenyu
2 days ago
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
2 days ago
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
2 days ago
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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