Skip to content

Instantly share code, notes, and snippets.

@urschrei
Last active August 29, 2015 13:57
Show Gist options
  • Save urschrei/9683723 to your computer and use it in GitHub Desktop.
Save urschrei/9683723 to your computer and use it in GitHub Desktop.
Calculate Jenks Natural Breaks on complete and NaN-containing datasets, using PySAL
from pysal.esda.mapclassify import Natural_Breaks as nb
# Calculate Jenks natural breaks
# Assumes that there are no NaN values in Series
breaks = nb(
df['column'].values,
initial=300,
k=5)
jb = pd.DataFrame({'jenks_bins': breaks.yb}, index=df['column'].index)
df = df.join(jb)
from pysal.esda.mapclassify import Natural_Breaks as nb
# Calculate Jenks natural breaks for Series which may have null values
breaks = nb(
df['column'].dropna().values,
initial=300,
k=5)
jb = pd.DataFrame({'jenks_bin': breaks.yb}, index=df['column'].dropna().index)
jenks = df.join(jb)
# This still leaves us with a number of NaN values, which we have to get rid of.
# the fillna() method creates a separate class of '-1' values,
# which will end up as the lightest colour when normalising
jenks['jenks_bin'].fillna(-1, inplace=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment