I am a little surprised to see that there seems to be no way to get the index of the active data frame in ArcPy, or did I miss something?

For example, what if you want to add a tool’s output to the active data frame? Here is the workflow I came up with to solve the problem:

  • Reference the MXD
  • Get the data frames
  • Get the name of the active data frame
  • Loop through all of the data frames, and check if the current one matches the name, if so, add the dataset

And here is the code:

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd,"*")
activeDfName = mxd.activeDataFrame.name

i = 0
for d in df:
    if d.name == activeDfName:
        finalDf = arcpy.mapping.ListDataFrames(mxd,"*")[i]
        newlayer = arcpy.mapping.Layer(addlayer)
        arcpy.mapping.AddLayer(finalDf, newlayer,"BOTTOM")
        i +=1

Although this works fine I feel like this is way too much code to do such a simple thing. Is there really no out of the box way to get the index of the active data frame, or can anyone suggest other ideas, perhaps more compact, how to approach this?

PS: parts of my question can be found here, but I think that my question is more clearly about whether this is possible at all, and it asks for alternatives, as it is always interesting to see what others come up with.

1 Answer 1


I think you're over-thinking this: mxd.activeDataFrame will give you the data frame itself, no need to get the name of it and then go searching for an index number. Check out the MapDocument documentation and the .activeDataFrame property of that class.

Extra note: On your iteration (which you don't need in this case) you can use enumerate(list) to reduce the number of lines. That function returns a list item as well as its index number in the list that you are iterating:

for index, item in enumerate(arcpy.mapping.ListDataFrames(mxd)):
    print index
    print item.name

will return


Comes in very handy sometimes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.