Skip to content
Competition - Bee friendly plants
  • AI Chat
  • Code
  • Report
  • 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.

    ⌛️ Time is ticking. Good luck!

    import pandas as pd
    data = pd.read_csv("data/plants_and_bees.csv", na_values="None")
    data.head()

    Which plants are preferred by native vs non-native bee species?

    In order to answer this question, we must normalize the data by the number of samples taken for each plant species. This will allow us to compare the relative frequency of each plant species in the samples.

    # Start coding here
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    native = data[(data["native_or_non"] == "native") & (~data["plant_species"].isna())]
    non_native = data[(data["native_or_non"] == "non-native") & (~data["plant_species"].isna())]
    
    # number of bees per species
    native_species_count = native["plant_species"].value_counts()
    non_native_species_count = non_native["plant_species"].value_counts()
    
    # number of samples unique per species
    native_samples_count = native.groupby("plant_species")["sample_id"].nunique()
    non_native_samples_count = non_native.groupby("plant_species")["sample_id"].nunique()
    
    # relative frequency of bees per species
    native_species_freq = native_species_count / native_samples_count
    non_native_species_freq = non_native_species_count / non_native_samples_count
    
    # convert to dataframe
    native_species_freq = pd.DataFrame(native_species_freq.sort_values(ascending=False), columns=["Relative Frequency"])
    non_native_species_freq = pd.DataFrame(non_native_species_freq.sort_values(ascending=False), columns=["Relative Frequency"])
    
    # display side to side
    preferred = pd.concat(
        [native_species_freq[:1], non_native_species_freq[:1]], 
        axis=0, 
        keys=["Native Bees", "Non-Native Bees"]).rename_axis(["Species Bees", "Plant Species"]).reset_index()
    preferred.head()
    

    How we can to see en before table the speceis of plant that are preferred by native bees is Rudbeckia hirta and the species of plant that are preferred by non-native bees is Cichorium intybus.

    A visualization of the distribution of bee and plant species across one of the samples.

    # drop na of plant species
    data_samples = data.dropna(subset=["plant_species"])
    
    
    # random sample
    sample_id = data_samples["sample_id"].sample().values[0]
    
    sample = data_samples[data_samples["sample_id"] == sample_id]
    sns.countplot(x="plant_species", data=sample, color="green")
    plt.title("Plant Species")
    plt.xlabel("Plant Species")
    plt.xticks(rotation=45)
    plt.show()

    Select the top three plant species you would recommend to the agency to support native bees.

    The top three plant species that I would recommend to the agency to support native bees are Rudbeckia Hirta, Rubeckia Triloba and Asclepias Tuberosa.

    top3_native = native_species_freq[:3]
    # graph top 3 native species
    sns.barplot(x=top3_native.index, y="Relative Frequency", data=top3_native, color="green")
    # x-axis label
    plt.xlabel("Plant Species")
    plt.title("Top 3 Native Plant Species")
    plt.xticks(rotation=45)
    plt.show()