Last active
August 29, 2015 13:57
-
-
Save urschrei/9683723 to your computer and use it in GitHub Desktop.
Calculate Jenks Natural Breaks on complete and NaN-containing datasets, using PySAL
This file contains hidden or 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
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) |
This file contains hidden or 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
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