Competition - Bee friendly plants
  • AI Chat
  • Code
  • Report
  • Spinner

    bees

    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.
    bees_numThe total number of bee individuals 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 plot.
    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)

    ⌛️ Time is ticking. Good luck!

    # import needed libraries 
    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    import numpy as np
    import plotly.express as px
    import plotly.graph_objects as go
    import copy
    import random
    import scipy.stats as stats
    import matplotlib.dates as mdates
    
    data = pd.read_csv("data/plants_and_bees.csv")
    data

    EDA

    Creating function checking the columns names, types , missing values number and number of unique values.

    
    # create an empty dataframe
    def creat_unique(df):
            
        df_unique = pd.DataFrame(columns=['Column_name','Data_type', 'Number_of_unique','Number_of_missing', 'Unique_values'])
    
        # loop through the columns in the other dataframe
        for col in df.columns:
            # get the number of unique values in the column
            num_unique = df[col].nunique()
    
            # add the unique values as a list to the 'Unique_values' column if num_unique <= 5
            if num_unique <= 15:
                unique_vals = list(df[col].unique())
            else:
                unique_vals = "More than 15 unique vales"
    
            # get the data type of the column
            data_type = df[col].dtype
    
            # count the number of missing values in the column
            num_missing = df[col].isnull().sum()
    
            # append a row to the empty dataframe with the column name, number of unique values, unique values, and data type
            df_unique = df_unique.append({'Column_name': col, 'Number_of_unique': num_unique, 'Unique_values': unique_vals, 'Data_type':
                                          data_type, 'Number_of_missing': num_missing}, ignore_index=True)
        return df_unique
    
    creat_unique(data)
    data.head()

    Creating another dataframe to keep the original and clean the new one

    cleaned_data = data.copy()
    
    # Convert 'date' column to datetime data type
    cleaned_data['date'] = pd.to_datetime(cleaned_data['date'])
    
    # Rest of the data cleaning steps...
    
    # Example: Print the updated data types
    print(cleaned_data['date'].dtypes)
    1- Replace the 'None' value in 'plant_species' column to 'Took from the air'
    cleaned_data['plant_species'] = cleaned_data['plant_species'].str.replace('None','Took from Air')
    cleaned_data['plant_species'].unique()