Skip to content

Instantly share code, notes, and snippets.

@sjtalkar
Last active October 29, 2021 17:41
Show Gist options
  • Select an option

  • Save sjtalkar/22ac2d07b9affa26af6eb41dc76d97c7 to your computer and use it in GitHub Desktop.

Select an option

Save sjtalkar/22ac2d07b9affa26af6eb41dc76d97c7 to your computer and use it in GitHub Desktop.
ArcGIS Functions
# Define a helper function to fix truncated zeros, with one parameter: the value to be processed
def fix_trunc_zeros(val):
# Use an if statement to check if there are four characters in the string representation of the value
if len(str(val)) == 4:
# If this is the case, return the value with an appended "0" in the front
return "0"+str(val)
# Otherwise...
else:
# Return the value itself
return str(val)
the built-in groupby function for the FIPS and year fields, which you use to group the data by candidate
# Use unstack to perform the table pivot, which will rotate the table and turn rows into columns
df_out = data_df.set_index(['FIPS',
'year',
'county',
'state',
'state_po',
'office',
data_df.groupby(['FIPS', 'year']).cumcount()+1]).unstack()
# Use the indexes for the columns to set column names (Ex: candidate_1, candidate_2, votes_1, votes_2, etc.)
df_out.columns = df_out.columns.map('{0[0]}_{0[1]}'.format)
# Rename columns
df_out = df_out.rename(columns={"candidate_1": "candidate_dem",
"candidatevotes_1": "votes_dem",
"candidate_2": "candidate_gop",
"candidatevotes_2": "votes_gop",
"totalvotes_1": "votes_total",
"state_po": "state_abbrev"
})
# Keep only the necessary columns
df_out = df_out[["candidate_dem", "votes_dem",
"candidate_gop", "votes_gop",
"votes_total"]]
# Remove the multiindex since we no longer need these fields to be "locked" for the pivot
df_out.reset_index(inplace=True)
# Print out the first few records to confirm everything worked
df_out.head()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment