Skip to content
0

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.

ColumnDescription
sample_idThe ID number of the sample taken.
species_numThe number of different bee species in the sample.
dateDate the sample was taken.
seasonSeason during sample collection ("early.season" or "late.season").
siteName of collection site.
native_or_nonWhether the sample was from a native or non-native plant.
samplingThe sampling method.
plant_speciesThe name of the plant species the sample was taken from. None indicates the sample was taken from the air.
timeThe time the sample was taken.
bee_speciesThe bee species in the sample.
sexThe gender of the bee species.
specialized_onThe plant genus the bee species preferred.
parasiticWhether or not the bee is parasitic (0:no, 1:yes).
nestingThe bees nesting method.
statusThe status of the bee species.
nonnative_beeWhether 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.

โŒ›๏ธ Time is ticking. Good luck!

import pandas as pd
data = pd.read_csv("data/plants_and_bees.csv")
data
# Start coding here
# Drop rows with 'None' in the 'plant_species' column
data = data[data['plant_species'] != 'None']

1. Preference of Plants by Native vs Non-Native Bee Species

# Native Bee Species
native_bees = data[data['nonnative_bee'] == 0]
native_plant_preference = native_bees['plant_species'].value_counts()

# Non-Native Bee Species
non_native_bees = data[data['nonnative_bee'] == 1]
non_native_plant_preference = non_native_bees['plant_species'].value_counts()

# Plants Preferred by Native Bee Species
plants_preferred_by_native_bees = native_plant_preference.index.tolist()

# Plants Preferred by Non-Native Bee Species
plants_preferred_by_non_native_bees = non_native_plant_preference.index.tolist()

print("Plants Preferred by Native Bee Species:")
for plant in plants_preferred_by_native_bees:
    print(plant)

print("\nPlants Preferred by Non-Native Bee Species:")
for plant in plants_preferred_by_non_native_bees:
    print(plant)

2. Visualization of Bee and Plant Species Distribution

import matplotlib.pyplot as plt

# Select a sample ID for visualization
sample_id = 17425  # Replace with the desired sample ID from your dataset

sample_data = data[data['sample_id'] == sample_id]
bee_species_counts = sample_data['bee_species'].value_counts()
plant_species_counts = sample_data['plant_species'].value_counts()

# Create a scatter plot of count of bee species and count of plant species in the sample
plt.figure(figsize=(10, 6))
plt.scatter(bee_species_counts.index, bee_species_counts.values)
plt.xlabel('Bee Species')
plt.ylabel('Count of Bee Species')
plt.title('Count of Bee Species in Sample {}'.format(sample_id))
plt.show()

plt.figure(figsize=(10, 6))
plt.scatter(plant_species_counts.index, plant_species_counts.values)
plt.xlabel('Plant Species')
plt.ylabel('Count of Plant Species')
plt.title('Count of Plant Species in Sample {}'.format(sample_id))
plt.show()
import numpy as np

# Select a sample ID for visualization
sample_id = 17425  # Replace with the desired sample ID from your dataset

# Filter data for the selected sample ID
sample_data = data[data['sample_id'] == sample_id]
bee_species_counts = sample_data['bee_species'].value_counts()
plant_species_counts = sample_data['plant_species'].value_counts()

# Prepare data for the grouped bar chart
species = sorted(set(bee_species_counts.index) | set(plant_species_counts.index))
bee_counts = [bee_species_counts.get(species, 0) for species in species]
plant_counts = [plant_species_counts.get(species, 0) for species in species]

# Set up the bar chart
x = range(len(species))
width = 0.35

fig, ax = plt.subplots(figsize=(10, 6))
rects1 = ax.bar(x, bee_counts, width, label='Bee Species')
rects2 = ax.bar(x, plant_counts, width, label='Plant Species', alpha=0.5)

# Add labels, title, and legend
ax.set_xlabel('Species')
ax.set_ylabel('Count')
ax.set_title('Distribution of Bee and Plant Species in Sample {}'.format(sample_id))
ax.set_xticks(x)
ax.set_xticklabels(species, rotation=45)
ax.legend()

# Display the chart
plt.tight_layout()
plt.show()
โ€Œ
โ€Œ
โ€Œ