Which plants are better for bees: native or non-native?
📖 Background
You work for the local government environment agency and have taken on a project about creating pollinator bee-friendly spaces. You can use both native and non-native plants to create these spaces and therefore need to ensure that you use the correct plants to optimize the environment for these bees.
The team has collected data on native and non-native plants and their effects on pollinator bees. Your task will be to analyze this data and provide recommendations on which plants create an optimized environment for pollinator bees.
💾 The Data
You have assembled information on the plants and bees research in a file called plants_and_bees.csv. Each row represents a sample that was taken from a patch of land where the plant species were being studied.
| Column | Description |
|---|---|
sample_id | The ID number of the sample taken. |
species_num | The number of different bee species in the sample. |
date | Date the sample was taken. |
season | Season during sample collection ("early.season" or "late.season"). |
site | Name of collection site. |
native_or_non | Whether the sample was from a native or non-native plant. |
sampling | The sampling method. |
plant_species | The name of the plant species the sample was taken from. None indicates the sample was taken from the air. |
time | The time the sample was taken. |
bee_species | The bee species in the sample. |
sex | The gender of the bee species. |
specialized_on | The plant genus the bee species preferred. |
parasitic | Whether or not the bee is parasitic (0:no, 1:yes). |
nesting | The bees nesting method. |
status | The status of the bee species. |
nonnative_bee | Whether the bee species is native or not (0:no, 1:yes). |
Source (data has been modified)
💪 Challenge
Provide your agency with a report that covers the following:
- Which plants are preferred by native vs non-native bee species?
- A visualization of the distribution of bee and plant species across one of the samples.
- Select the top three plant species you would recommend to the agency to support native bees.
✅ Checklist before publishing
- Rename your workspace to make it descriptive of your work. N.B. you should leave the notebook name as notebook.ipynb.
- Remove redundant cells like the judging criteria, so the workbook is focused on your work.
- Check that all the cells run without error.
import pandas as pd
data = pd.read_csv("data/plants_and_bees.csv")
data# Start coding here
# Import packages
import matplotlib.pyplot as plt
import random
# Data Exploring
data = data[data['plant_species'] != 'None']
data.info()# Analyze preferences of native and non-native bee species
# Group the data by 'native_or_non' column and count the occurrences of each plant species
preferences = data.groupby('native_or_non')['plant_species'].value_counts()
print(preferences)# Analyze preferences of native and non-native plant species
native_plant_counts = data[data['native_or_non'] == 'native']['plant_species'].value_counts()
non_native_plant_counts = data[data['native_or_non'] == 'non-native']['plant_species'].value_counts()
# A bar plot to visualize the preferences of native and non-native plant species
plt.figure(figsize=(10, 6))
plt.bar(native_plant_counts.index, native_plant_counts.values, label='Native Plants', color='green')
plt.bar(non_native_plant_counts.index, non_native_plant_counts.values, label='Non-Native Plants', color='blue')
plt.xlabel('Plant Species')
plt.ylabel('Count')
plt.title('Preferences of Native and Non-Native Plant Species')
plt.legend()
plt.xticks(rotation=90)
for i, value in enumerate(native_plant_counts.values):
plt.annotate(str(value), xy=(i, value), ha='top', color='white')
for i, value in enumerate(non_native_plant_counts.values):
plt.annotate(str(value), xy=(i, value), ha='top', color='white')
plt.show()# Visualize the distribution of bee and plant species across a sample
# Select a sample at random to visualize
sample_id = random.choice(data['sample_id'])
sample_data = data[data['sample_id'] == sample_id]
# Count the occurrences of each bee species in the sample
bee_counts = sample_data['bee_species'].value_counts()
# Count the occurrences of each plant species in the sample
plant_counts = sample_data['plant_species'].value_counts()
# Bar plot to visualize the distribution of bee and plant species
plt.figure(figsize=(10, 6))
plt.bar(bee_counts.index, bee_counts.values, label='Bee Species')
plt.bar(plant_counts.index, plant_counts.values, label='Plant Species')
plt.xlabel('Species')
plt.ylabel('Count')
plt.title(f'Distribution of Bee and Plant Species (Sample ID: {sample_id})')
plt.legend()
plt.xticks(rotation=90)
plt.show()# Top three plant species recommended for supporting native bees
# Filter the data for native bee species
native_bees_data = data[data['native_or_non'] == 'native']
# Group the data by plant species and count the occurrences of each species
plant_counts_native = native_bees_data['plant_species'].value_counts()
# Top three plant species with the highest occurrences
top_three_plants = plant_counts_native.head(3)
# Print the top three plant species recommended for supporting native bees
print(top_three_plants)