Tuesday, June 11, 2024

Exploring and Manipulating Data with Python

This week we really got into coding! The assignment included using python to look at and edit data. We had to write a code that created a new geodatabase, copied files over to it, sort data from a layer, and then print this information out in several ways. 


Here's a look at the script workflow.

Most of the code was pretty easy and straightforward as the exercise covered the basics well. I ran into issues with the search cursors. Creating them was easy enough but getting the data from the attribute table into the desired format was more tricky.

I had issues trying to print the results from the first search. The population is acting as an integer so it did not want to print when I create the code for this. I remembered issues with this situation from Module 2 and looked back at this code to see how I fixed it. I used a command to turn the integer into a string – str(). However, it still took me some playing around to figure out how to put this into the code. I tried it when I assigned the field to my variable pop, and then in several places around the print code before I finally sorted it out – str(row.getValue(pop)). 

Similarly, I created the search cursor for the dictionary easily enough but populating it, on the other hand, gave me trouble! I tried to reuse the search cursor settings but it only returns one city – Carlsbad, which is the last on the list. I finally figured out I needed to change the way that I was writing the code to add the keys and values to the dictionary. While {} creates a dictionary, I could add values in a simpler fashion – county_seats[key] = value. The most foolish part is I realized this was explained in the assignment instructions when I went to read back over them a little more thoroughly.

Overall, I feel like I'm getting the hang of coding and am enjoying the challenge of it.

Here's a look at the results of my code:
























No comments:

Post a Comment

GIS Portfolio

To show off all I have learned during my GIS Graduate Certificate program, I created an online portfolio. Click here to check it out.  The ...