Guest User

Untitled

a guest
May 25th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. # coding: utf-8
  2.  
  3. # In[1]:
  4.  
  5.  
  6. from matplotlib import pyplot as plt
  7.  
  8.  
  9. # In[2]:
  10.  
  11.  
  12. import pandas as pd
  13.  
  14.  
  15. # In[4]:
  16.  
  17.  
  18. species = pd.read_csv('species_info.csv')
  19.  
  20.  
  21. # In[10]:
  22.  
  23.  
  24. species.head()
  25.  
  26.  
  27. # In[37]:
  28.  
  29.  
  30. species.info()
  31.  
  32.  
  33. # In[40]:
  34.  
  35.  
  36. species_type = species.category.unique()
  37. print species_type
  38.  
  39.  
  40. # In[39]:
  41.  
  42.  
  43. conservation_info = species.conservation_status.unique()
  44. print conservation_info
  45.  
  46.  
  47. # In[41]:
  48.  
  49.  
  50. conservation_num = species.groupby('conservation_status').scientific_name.count().reset_index()
  51. print conservation_num
  52.  
  53.  
  54. # In[47]:
  55.  
  56.  
  57. species.fillna('No Intervention', inplace=True)
  58.  
  59.  
  60. # In[48]:
  61.  
  62.  
  63. conservation_num_np = species.groupby('conservation_status').scientific_name.count().reset_index()
  64. print conservation_num_np
  65.  
  66.  
  67. # In[90]:
  68.  
  69.  
  70. protection_counts = species.groupby('conservation_status') .scientific_name.nunique().reset_index() .sort_values(by='scientific_name')
  71. print protection_counts
  72.  
  73.  
  74. # In[51]:
  75.  
  76.  
  77. plt.figure(figsize=(10, 4))
  78. ax = plt.subplot()
  79. plt.bar(range(len(protection_counts)),
  80. protection_counts.scientific_name.values)
  81. ax.set_xticks(range(len(protection_counts)))
  82. ax.set_xticklabels(protection_counts.conservation_status.values)
  83. plt.ylabel('Number of Species')
  84. plt.title('Conservation Status by Species')
  85. labels = [e.get_text() for e in ax.get_xticklabels()]
  86. print ax.get_title()
  87. plt.show()
  88.  
  89.  
  90. # In[89]:
  91.  
  92.  
  93. species['is_protected'] = species.conservation_status != 'No Intervention'
  94.  
  95.  
  96. # In[91]:
  97.  
  98.  
  99. category_counts = species.groupby(['category', 'is_protected']).scientific_name.nunique().reset_index()
  100. category_counts.head(7)
  101.  
  102.  
  103. # In[60]:
  104.  
  105.  
  106. category_pivot = category_counts.pivot(columns='is_protected', index='category', values='scientific_name').reset_index()
  107. category_pivot.head()
  108.  
  109.  
  110. # In[103]:
  111.  
  112.  
  113. category_pivot.columns = ['category', 'not_protected', 'protected']
  114. print category_pivot.columns
Add Comment
Please, Sign In to add comment