Skip to content

Instantly share code, notes, and snippets.

@ccerv1
ccerv1 / cagr.py
Created February 16, 2019 12:20
Creates a CAGR value (as a formatted string)
def cagr(series):
start_val = series.iloc[0]
end_val = series.iloc[-1]
num_vals = len(series)
CAGR = (end_val/start_val)**(1/num_vals)-1
prefix = ''
if CAGR > 0:
prefix = '+'
return prefix + '{:.1%}'.format(CAGR)
@ccerv1
ccerv1 / pretty_production_chart_v3.py
Created February 16, 2019 12:22
Adds CAGR to the labels and a title that explains the key message
labels = []
y_vals = []
cum_sum = 0
for col_num, country_name in enumerate(production_table.columns):
label = country_name + " " + cagr(production_table[country_name])
labels.append(label)
prod_val = production_table.iloc[-1,col_num]
y_vals.append((cum_sum * 2 + prod_val)/2)
cum_sum += prod_val
@ccerv1
ccerv1 / geopandas_basemap.py
Created February 23, 2019 12:48
Display the default GeoPandas base map of the world
import geopandas
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
world.plot()
@ccerv1
ccerv1 / world_map.py
Created February 23, 2019 13:14
Create a new table with relevant features only
world_map = world[world.name != 'Antarctica']
world_map = world_map[['name', 'geometry']]
world_map.set_index('name', inplace=True)
@ccerv1
ccerv1 / pretty_world_map.py
Created February 23, 2019 13:16
Pretty version of the GeoPandas world map
fig, ax = plt.subplots(figsize=(20,8))
ax.set_aspect('equal')
world_map.plot(color='#f2f2f2', edgecolor="Grey", ax=ax)
@ccerv1
ccerv1 / world_production.py
Created February 23, 2019 13:21
Extract world production over the last 25 years from USDA table
world_production = attribute_query('Production')
world_production.fillna(0,inplace=True)
world_production = world_production.iloc[:,-YEARS_IN_SERIES:]
@ccerv1
ccerv1 / country_rename.py
Created February 23, 2019 13:30
Rename country indices that have alternative spellings
world_production.rename({
"Cote d'Ivoire": "Côte d'Ivoire",
"Central African Republic": "Central African Rep.",
"Congo (Kinshasa)": "Dem. Rep. Congo",
"Dominican Republic": "Dominican Rep.",
"Laos": "Lao PDR"
}, inplace=True)
@ccerv1
ccerv1 / world_production_cagr.py
Created February 23, 2019 13:43
Apply a CAGR function to each country's production series
def cagr(series):
start_val = series.iloc[0]
if not start_val:
return None
end_val = series.iloc[-1]
num_vals = len(series)
CAGR = (end_val/start_val)**(1/num_vals)-1
return CAGR
world_production['CAGR'] = world_production.apply(cagr, axis=1)
@ccerv1
ccerv1 / cagr_choropleth.py
Created February 23, 2019 13:49
V1 choropleth map of coffee production growth rates
cagr_map = pd.concat([world_map, world_production], axis=1, sort=True)
cagr_map.fillna(0, inplace=True)
cagr_map.plot(column='CAGR', cmap='Blues')
@ccerv1
ccerv1 / cagr_choropleth_v2.py
Created February 23, 2019 17:34
Pretty version of the CAGR choropleth map of coffee production
growing_map = cagr_map[cagr_map.CAGR > 0]
growing_map.plot(column='CAGR', cmap='YlGn', scheme='fisher_jenks', edgecolor='black', ax=ax)
shrinking_map = cagr_map[cagr_map.CAGR < 0]
shrinking_map.plot(column='CAGR', cmap='Reds_r', scheme='fisher_jenks', edgecolor='black', ax=ax)
ax.set_ylim([-23.5,23.5])
ax.set_xlim([-120,160])
fig