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)
๐งโโ๏ธ Judging criteria
This is a community-based competition. The top 5 most upvoted entries will win.
The winners will receive DataCamp merchandise.
โ
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")
dataimport pandas as pd
import matplotlib.pyplot as plt
# Load the data
filename = "data/plants_and_bees.csv"
df = pd.read_csv(filename)
# Assuming the sample data is stored in a DataFrame called 'df'
# Drop any unnecessary columns
df = df.drop(columns=["species_num", "date", "season", "site", "native_or_non", "sampling", "time", "sex",
"specialized_on", "parasitic", "nesting", "status", "nonnative_bee"])
# Convert relevant columns to categorical data type
df["plant_species"] = df["plant_species"].astype("category")
df["bee_species"] = df["bee_species"].astype("category")
# Convert the remaining columns to appropriate data types if needed
# df["sample_id"] = df["sample_id"].astype(int)
# Reset the index of the DataFrame
df = df.reset_index(drop=True)
# Display the preprocessed DataFrame
print(df)
# Filter the dataset based on native and non-native bee species
native_bees = data[data["native_or_non"] == "native"]
non_native_bees = data[data["native_or_non"] == "non-native"]
# Calculate plant preferences for native and non-native bees
native_bee_plant_preference = native_bees["plant_species"].value_counts()
non_native_bee_plant_preference = non_native_bees["plant_species"].value_counts()
# Display the top preferred plants for native and non-native bees
print("Native bee plant preference:")
print(native_bee_plant_preference.head())
print("\nNon-native bee plant preference:")
print(non_native_bee_plant_preference.head())# Assuming the sample data is stored in a DataFrame called 'df'
# Select the desired sample ID
sample_id = 17400
# Filter the data for the selected sample ID
sample = df[df["sample_id"] == sample_id]
# Count the occurrences of bee and plant species
bee_counts = sample["bee_species"].value_counts()
plant_counts = sample["plant_species"].value_counts()
# Create a bar plot of bee and plant species distribution
fig, ax = plt.subplots()
bee_counts.plot(kind="bar", ax=ax, label="Bee Species")
plant_counts.plot(kind="bar", ax=ax, label="Plant Species")
# Set the labels and title
ax.set_xlabel("Species")
ax.set_ylabel("Count")
ax.set_title("Distribution of Bee and Plant Species in Sample {}".format(sample_id))
# Show the plot
plt.legend()
plt.show()# Select the top three plant species to support native bees
top_plants = native_bee_plant_preference.head(3).index.tolist()
print("Top three plant species to support native bees:")
print(", ".join(top_plants))