Beta
Which tree species should the city plant?
📖 Background
You work for a nonprofit organization advising the planning department on ways to improve the quantity and quality of trees in New York City. The urban design team believes tree size (using trunk diameter as a proxy for size) and health are the most desirable characteristics of city trees.
The city would like to learn more about which tree species are the best choice to plant on the streets of Manhattan.
💾 The data
The team has provided access to the 2015 tree census and geographical information on New York City neighborhoods (trees, neighborhoods):
Tree Census
- "tree_id" - Unique id of each tree.
- "tree_dbh" - The diameter of the tree in inches measured at 54 inches above the ground.
- "curb_loc" - Location of the tree bed in relation to the curb. Either along the curb (OnCurb) or offset from the curb (OffsetFromCurb).
- "spc_common" - Common name for the species.
- "status" - Indicates whether the tree is alive or standing dead.
- "health" - Indication of the tree's health (Good, Fair, and Poor).
- "root_stone" - Indicates the presence of a root problem caused by paving stones in the tree bed.
- "root_grate" - Indicates the presence of a root problem caused by metal grates in the tree bed.
- "root_other" - Indicates the presence of other root problems.
- "trunk_wire" - Indicates the presence of a trunk problem caused by wires or rope wrapped around the trunk.
- "trnk_light" - Indicates the presence of a trunk problem caused by lighting installed on the tree.
- "trnk_other" - Indicates the presence of other trunk problems.
- "brch_light" - Indicates the presence of a branch problem caused by lights or wires in the branches.
- "brch_shoe" - Indicates the presence of a branch problem caused by shoes in the branches.
- "brch_other" - Indicates the presence of other branch problems.
- "postcode" - Five-digit zip code where the tree is located.
- "nta" - Neighborhood Tabulation Area (NTA) code from the 2010 US Census for the tree.
- "nta_name" - Neighborhood name.
- "latitude" - Latitude of the tree, in decimal degrees.
- "longitude" - Longitude of the tree, in decimal degrees.
Neighborhoods' geographical information
- "ntacode" - NTA code (matches Tree Census information).
- "ntaname" - Neighborhood name (matches Tree Census information).
- "geometry" - Polygon that defines the neighborhood.
Tree census and neighborhood information from the City of New York NYC Open Data.
The work was done in the Data camp workspace, but when it was published, I was forced to switch to another workspace because there was a problem with displaying maps that I had not defeated
import pandas as pd
import geopandas as gpd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import folium
from PIL import Image
sns.set()
trees = pd.read_csv('data/trees.csv')
trees
neighborhoods = gpd.read_file('data/nta.shp')
neighborhoods
neighborhoods.drop_duplicates()
trees.drop_duplicates()
def unique_value(df):
variables = pd.DataFrame(columns=['Variable','Number of unique values','Type data', 'Values'])
for i, var in enumerate(df.columns):
variables.loc[i] = [var, df[var].nunique(),df.dtypes[i] ,df[var].unique().tolist()]
variables.set_index('Variable', inplace=True)
return variables
unique_value(trees)
unique_value(neighborhoods)
def percent_hbar(df, old_threshold=None):
percent_of_nulls = (df.isnull().sum()/len(df)*100).sort_values().round(2)
threshold = percent_of_nulls.mean()
ax = percent_of_nulls.plot(kind='barh', figsize=(20, 16), title='% of NaN (from {} lines)'.format(len(df)),
color='#86bf91', legend=False, fontsize=17)
ax.set_xlabel('Count of NaN')
dict_percent = dict(percent_of_nulls)
i = 0
for k in dict_percent:
color = 'blue'
if dict_percent[k] > 0:
if dict_percent[k] > threshold:
color = 'red'
ax.text(dict_percent[k]+0.1, i + 0.09, str(dict_percent[k])+'%', color=color,
fontweight='bold', fontsize='large')
i += 0.98
if old_threshold is not None:
plt.axvline(x=old_threshold,linewidth=1, color='r', linestyle='--')
ax.text(old_threshold+0.3, .10, '{0:.2%}'.format(old_threshold/100), color='r', fontweight='bold', fontsize='large')
plt.axvline(x=threshold,linewidth=1, color='green', linestyle='--')
ax.text(threshold+0.3, .7, '{0:.2%}'.format(threshold/100), color='green', fontweight='bold', fontsize='large')
else:
plt.axvline(x=threshold,linewidth=1, color='r', linestyle='--')
ax.text(threshold+0.3, .7, '{0:.2%}'.format(threshold/100), color='r', fontweight='bold', fontsize='large')
ax.set_xlabel('')
return ax, threshold
plot, threshold = percent_hbar(trees)
Let's take a closer look at our missing data.
trees[trees['health'].isna()==True]
print('Alive trees in Manhatten {}\nDead trees in Manhatten {}'.format(trees.status.value_counts()[0],trees.status.value_counts()[1]))
‌
‌
‌
‌
‌