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. |
bees_num | The total number of bee individuals 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 plot. |
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)
⌛️ 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()