Created
September 22, 2013 17:45
-
-
Save urschrei/6662164 to your computer and use it in GitHub Desktop.
Calculate Jenks natural breaks on a dataset containing zero values, using Pandas and Pysal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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