Skip to content

Instantly share code, notes, and snippets.

@urschrei
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urschrei/5794224526a470a8f0b0 to your computer and use it in GitHub Desktop.
Save urschrei/5794224526a470a8f0b0 to your computer and use it in GitHub Desktop.
Loop through a grouped, time-series-indexed Pandas DataFrame, plotting category counts per year
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# this data won't work (it's only for one year), it's only illustrative
df = pd.DataFrame({
'Age': {0: 32, 1: 38, 2: 45, 3: 39, 4: 44},
'County': {0: 'Bexar', 1: 'Tarrant', 2: 'Harris', 3: 'Nueces', 4: 'Kerr'},
'Date Executed': {
0: '10/28/2014',
1: '09/17/2014',
2: '09/10/14',
3: '04/16/2014',
4: '04/09/14'},
'Executed': {0: 518, 1: 517, 2: 516, 3: 515, 4: 514},
'First Name': {0: 'Miguel', 1: 'Lisa', 2: 'Willie', 3: 'Jose', 4: 'Ramiro'},
'Last Name': {
0: 'Paredes',
1: 'Coleman',
2: 'Trottie',
3: 'Villegas',
4: 'Hernandez'},
'Race': {0: 'Hispanic', 1: 'Black', 2: 'Black', 3: 'Hispanic', 4: 'Hispanic'},
'TDCJ Number': {0: 999400, 1: 999511, 2: 999085, 3: 999417, 4: 999342}})
# convert to DateTime
df['Date Executed'] = pd.to_datetime(df['Date Executed'], dayfirst=True, infer_datetime_format = True)
# set as index
df.set_index('Date Executed', inplace=True)
# set Race as categorical
df['Race'] = df['Race'].astype('category')
grouped = df.groupby('Race')
plt.clf()
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, axisbg='w', frame_on=True)
for key, group in grouped:
data = group.groupby(lambda x: x.year).count()
data['Race'].plot(label=key, ax=ax, lw=2.)
# label x and y axis
plt.xlabel("Year")
plt.ylabel("Number of Executions")
# turn off background grid
ax.grid(b=None)
# show a semi-transparent legend
leg = ax.legend(loc='best')
leg.get_frame().set_alpha(0.75)
# set a title
plt.title(
"Executions per Race, %s - %s" % (min(df.index.year), max(df.index.year)),
fontweight='bold')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment