Python Forum
ARCPY enter excel table into coded domain
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ARCPY enter excel table into coded domain
#1
This is what i have been trying so far, i cant seem to get the dictionary in the right form.
Appreciate any help i can get

>>> import arcpy
>>> from arcpy import env
>>> env.workspace = 'C:\Milgeo\python'
>>> arcpy.CreateDomain_management('presidentos.gdb', 'amerikanske presidenter', 'amerikas presidenter fra Washington til Trump', "SHORT", "CODED")
<Result 'C:\\Milgeo\\python\\presidentos.gdb'>
>>> import pandas
>>> import pandas as pd
>>> file_path = 'C:\Milgeo\python\president.xlsx'
>>> df = pd.read_excel(file_path, encoding = 'utf-16')
>>> df.to_dict()
{u'Presidenter': {0: u'George Washington', 1: u'John Adams', 2: u'Thomas Jefferson', 3: u'James Madison', 4: u'James Monroe', 5: u'John Quincy Adams', 6: u'Andrew Jackson', 7: u'Martin Van Buren', 8: u'William H. Harrison', 9: u'John Tyler', 10: u'James Knox Polk', 11: u'Zachary Taylor', 12: u'Millard Fillmore', 13: u'Franklin Pierce', 14: u'James Buchanan', 15: u'Abraham Lincoln', 16: u'Andrew Johnson', 17: u'Ulysses S. Grant', 18: u'Rutherford B. Hayes', 19: u'James Garfield', 20: u'Chester A. Arthur', 21: u'Grover Cleveland', 22: u'Benjamin Harrison', 23: u'Grover Cleveland', 24: u'William McKinley', 25: u'Theodore Roosevelt', 26: u'William H. Taft', 27: u'Woodrow Wilson', 28: u'Warren G. Harding', 29: u'Calvin Coolidge', 30: u'Herbert Hoover', 31: u'Franklin D. Roosevelt', 32: u'Harry S. Truman', 33: u'Dwight D. Eisenhower', 34: u'John F. Kennedy', 35: u'Lyndon B. Johnson', 36: u'Richard Nixon', 37: u'Gerald Ford', 38: u'Jimmy Carter', 39: u'Ronald Reagan', 40: u'George H.W. Bush', 41: u'Bill Clinton', 42: u'George W. Bush', 43: u'Barack Obama', 44: u'Donald J. Trump'}}
>>> for code in df.to_dict():
...     arcpy.AddCodedValueToDomain_management('presidentos.gdb', "amerikanske presidenter", 'code', 'df.to_dict[code]')
...
Error:
Runtime error Traceback (most recent call last): File "<string>", line 2, in <module> File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\management.py", line 1492, in AddCodedValueToDomain raise e ExecuteError: ERROR 000378: Invalid Short type. >>>
Reply
#2
arcpy.AddCodedValueToDomain_management('presidentos.gdb', "amerikanske presidenter", 'code', 'df.to_dict[code]')
should be
arcpy.AddCodedValueToDomain_management('presidentos.gdb', "amerikanske presidenter", code, df.to_dict[code])
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
By the way, this looks like a lot of typing in interactive mode. Why not save it as py file and run the script? So you will be able to reuse it whenever you need
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
I tried this and got the following error:
Error:
Runtime error Traceback (most recent call last): File "<string>", line 2, in <module> TypeError: 'instancemethod' object has no attribute '__getitem__'
Reply
#5
Sorry, I miss the parenthesis after to_dict, i.e. the last argument should be
df.to_dict()[code]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(Feb-22-2019, 08:41 PM)buran Wrote: By the way, this looks like a lot of typing in interactive mode. Why not save it as py file and run the script? So you will be able to reuse it whenever you need
By the way i just started using python to simplify some of my work. And i have no education on it what so ever, so I really appreciate any kind of help!
Reply
#7
take a look at
https://python-forum.io/Thread-How-to-Ex...ython-code

You can write your code in any texteditor (e.g. Notepad, but not Word). However better look at some real IDE, that will have added benefits like code highlighting, code completion, debugger, linter, etc. I would recommned VS Code.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Peaks in time domain frohr 0 451 Jul-06-2023, 02:57 AM
Last Post: frohr
  Python script to summarize excel tables, then output a composite table? i'm a total n surfer349 1 2,290 Feb-05-2021, 04:37 PM
Last Post: nilamo
  I try to import data from Excel table to Word Template. NewbiePyPy 0 3,220 Oct-21-2020, 12:25 PM
Last Post: NewbiePyPy
  Save output into a Excel Sheet with Format Table skaailet 1 2,455 Apr-17-2020, 11:56 PM
Last Post: thirteendec
  Iterating an attribute table using Arcpy teflon 6 3,983 Oct-28-2019, 09:15 PM
Last Post: teflon
  Converting a patterned string or text into excel table soup1987 1 2,096 Oct-03-2019, 01:37 AM
Last Post: Larz60+
  setting environment in arcpy sunny 1 2,783 Feb-25-2019, 06:13 PM
Last Post: sunny
  Avoid hard-coded folderPath in the code (let users provide it) SunOffice024 6 5,226 Sep-08-2018, 06:41 PM
Last Post: SunOffice024
  Converting IP 2 DOMAIN jetli2 8 4,351 May-25-2018, 03:29 PM
Last Post: jetli2
  frequency domain plot jkeertir 0 2,718 Mar-05-2018, 06:54 AM
Last Post: jkeertir

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020