Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calculate Jenks natural breaks on a dataset containing zero values, using Pandas and Pysal
import pandas as pd
import numpy as np
from pysal.esda.mapclassify import Natural_Breaks as nb
df = pd.DataFrame({'density': np.random.randint(0, 10, 500)})
# replace zero values with NaN
df.replace(to_replace={'density': {0: np.nan}}, inplace=True)
breaks = nb(df[df['density'].notnull()].density.values, k=5)
# this index will allow us to perform the join correctly
jb = pd.DataFrame(
{'jenks_breaks': breaks.yb},
index=df[df['density'].notnull()].index)
df = df.join(jb)
# fill the zero-value rows with -1
df.jenks_breaks.fillna(-1, inplace=True)
labels = breaks.bins
# jenks_breaks now has correct bin values, with -1 denoting zero-valued densities
# this can be used to colour a matplotlib PatchCollection by using set_facecolor and a Normalize instance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment