From 4781f1d91e8c07c5a565455acc76ca640b462920 Mon Sep 17 00:00:00 2001 From: Pascal Bisson Date: Thu, 20 Jun 2024 20:58:44 -0400 Subject: [PATCH 1/6] Added .gitIgnore to prevent unprotected data from being committed. --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..05cd75e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +**.docx +**.csv +/venv/ +.idea \ No newline at end of file From 86c7348cfe38ffdadd2fc3eafc9a04fba3564f3c Mon Sep 17 00:00:00 2001 From: Pascal Bisson Date: Thu, 20 Jun 2024 21:03:24 -0400 Subject: [PATCH 2/6] Added Lib python package with module data_loader.py This module contains three methods to load csv data - get_data_as_pandas_data_frame(file_path: str) - get_data_as_numpy_array(file_path: str) - get_csv_reader(file_path: str) --- Lib/__init__.py | 0 Lib/console.py | 22 +++++++++ Lib/data_loader.py | 109 +++++++++++++++++++++++++++++++++++++++++++++ README.md | 16 ++++++- constants.py | 7 +++ main.py | 24 ++++++++++ 6 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 Lib/__init__.py create mode 100644 Lib/console.py create mode 100644 Lib/data_loader.py create mode 100644 constants.py create mode 100644 main.py diff --git a/Lib/__init__.py b/Lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Lib/console.py b/Lib/console.py new file mode 100644 index 0000000..ad8a690 --- /dev/null +++ b/Lib/console.py @@ -0,0 +1,22 @@ + +class _Colours: + # default + RESET = '\033[0m' + # warning + YELLOW = '\033[93m' + # error + RED = '\033[91m' + + GREEN = '\033[92m' + BLUE = '\033[94m' + PURPLE = '\033[95m' + CYAN = '\033[96m' + WHITE = '\033[97m' + + +def print_warn(msg: str): + print(_Colours.YELLOW + "Warning: " + msg + _Colours.RESET) + + +def print_error(msg: str): + print(_Colours.RED + "Error: " + msg + _Colours.RESET) \ No newline at end of file diff --git a/Lib/data_loader.py b/Lib/data_loader.py new file mode 100644 index 0000000..24905f4 --- /dev/null +++ b/Lib/data_loader.py @@ -0,0 +1,109 @@ +import os +from typing import Optional, Union +from Lib.console import print_warn, print_error + +# Attempt to import csv +try: + import csv +except ImportError: + csv = None + print_warn("Warning: csv module is not available. CSV file handling will not be supported.") + +# Attempt to import pandas +try: + import pandas as pd +except ImportError: + pd = None + print_warn("Warning: pandas library is not available. Data frame functionality will not be supported.") + +# Attempt to import numpy +try: + import numpy as np +except ImportError: + np = None + print_warn("numpy library is not available. NumPy array functionality will not be supported.") + + +# Define functions using imported libraries, checking for None before usage +def get_data_as_pandas_data_frame(file_path: str): + """ + Load a CSV file into a pandas DataFrame. + + Parameters: + file_path (str): The path to the CSV file. + + Returns: + pd.DataFrame: The loaded DataFrame, or an empty DataFrame if loading fails. + """ + if pd is None: + print_error("pandas library is not available.") + return None + + try: + if not os.path.isfile(file_path): + raise FileNotFoundError(f"'{file_path}' is not a valid file path.") + + if not file_path.endswith('.csv'): + raise ValueError(f"'{file_path}' does not point to a CSV file.") + + df = pd.read_csv(file_path, encoding='utf-8') + return df + + except Exception as e: + print_error(f"An error occurred while loading the CSV file: {e}") + return pd.DataFrame() + + +def get_data_as_numpy_array(file_path: str): + """ + Load data from a text file into a NumPy array. + + Parameters: + file_path (str): The path to the text file. + + Returns: + np.ndarray: The loaded NumPy array, or an empty array if loading fails. + """ + if np is None: + print_error("numpy library is not available.") + return None + + try: + if not os.path.isfile(file_path): + raise FileNotFoundError(f"Error: '{file_path}' is not a valid file path.") + + # Example assuming data is space-delimited + data = np.loadtxt(file_path) + return data + + except Exception as e: + print_error(f"An error occurred while loading the data: {e}") + return np.array([]) + + +def get_csv_reader(file_path: str): + """ + Load data from a CSV file and return a CSV reader object. + + Parameters: + file_path (str): The path to the CSV file. + + Returns: + csv.reader: A CSV reader object. Returns an empty reader if loading fails. + """ + if csv is None: + print_error("csv library is not available.") + return None + try: + if not os.path.isfile(file_path): + raise FileNotFoundError(f"'{file_path}' is not a valid file path.") + + # Open the CSV file and return the CSV reader object + with open(file_path, 'r') as file: + reader = csv.reader(file) + return reader + + except Exception as e: + print_error(f"An error occurred while loading the CSV file: {e}") + # Return an empty CSV reader if an exception occurs + return csv.reader([]) # Return an empty iterable (empty CSV reader) diff --git a/README.md b/README.md index 3bb48e2..a955273 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,23 @@ -# Ottawa Food Bank +# Ottawa Food Bank - Quick Start Python ## Research Questions 1. What is the relationship between food security status and health/demographics/needs in our sample? 2. Considering the stated needs of people accessing food programs, do agencies/the network have the capacity to meet those needs? +## Branch Details +You can use this branch to help you get started if you prefer working in Python. + +It contains very basic and simple methods to help you load data. + +All you need to do is extract the both zip files into the `Data` folder. + +- ### Lib + - #### data_loader + - get_data_as_pandas_data_frame(file_path: str) + - get_data_as_numpy_array(file_path: str) + - get_csv_reader(file_path: str) + + ## Data Details In this repository, you will find two password-protected ZIP files containing survey data: diff --git a/constants.py b/constants.py new file mode 100644 index 0000000..2e63a31 --- /dev/null +++ b/constants.py @@ -0,0 +1,7 @@ +# Lib/constants.py + +import os + +# Define the paths to CSV files +PathHungerCount: str = os.path.join(os.path.dirname(__file__), 'Data/hunger_count_cleaned-2024-06-14.csv') +PathNeighbourSurvey: str = os.path.join(os.path.dirname(__file__), 'Data/neighbour_survey_clean-2024-06-14.csv') diff --git a/main.py b/main.py new file mode 100644 index 0000000..6bc9661 --- /dev/null +++ b/main.py @@ -0,0 +1,24 @@ +from constants import PathNeighbourSurvey, PathHungerCount +from Lib import data_loader as dl + + +def main(): + + # Example: Load the CSV file into a pandas DataFrame using the function + df_neighbour_survey = dl.get_data_as_pandas_data_frame(PathNeighbourSurvey) + df_hunger_count = dl.get_data_as_pandas_data_frame(PathHungerCount) + + # Display the first few rows of the DataFrame to verify the load + if not df_neighbour_survey.empty: + print(df_neighbour_survey.head()) + else: + print("Failed to load [Neighbour Survey] data.") + + if not df_hunger_count.empty: + print(df_hunger_count.head()) + else: + print("Failed to load [Hunger Count] data.") + + +if __name__ == "__main__": + main() \ No newline at end of file From 8c39cca06cf76702070ff745c51b9184ba6d0c81 Mon Sep 17 00:00:00 2001 From: Pascal Bisson Date: Thu, 20 Jun 2024 21:40:29 -0400 Subject: [PATCH 3/6] updated get_csv_reader to target utf-8 and reworked get_data_as_numpy_array and --- Lib/data_loader.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Lib/data_loader.py b/Lib/data_loader.py index 24905f4..810ae13 100644 --- a/Lib/data_loader.py +++ b/Lib/data_loader.py @@ -72,10 +72,18 @@ def get_data_as_numpy_array(file_path: str): if not os.path.isfile(file_path): raise FileNotFoundError(f"Error: '{file_path}' is not a valid file path.") - # Example assuming data is space-delimited - data = np.loadtxt(file_path) - return data + with open(file_path, 'r', encoding='utf-8') as file: + # Read all lines from the file + lines = file.readlines() + # Extract headers and data + headers = lines[0].strip().split(',') + data_lines = [line.strip().split(',') for line in lines[1:]] + + # Convert data_lines to a numpy array + numpy_array = np.array(data_lines, dtype=object) # Assuming data is numeric + + return numpy_array except Exception as e: print_error(f"An error occurred while loading the data: {e}") return np.array([]) @@ -99,7 +107,7 @@ def get_csv_reader(file_path: str): raise FileNotFoundError(f"'{file_path}' is not a valid file path.") # Open the CSV file and return the CSV reader object - with open(file_path, 'r') as file: + with open(file_path, 'r', encoding='utf-8') as file: reader = csv.reader(file) return reader From 21d8ed0441ba18af86beff9447ede420f11c21a5 Mon Sep 17 00:00:00 2001 From: CalvinTC97 Date: Tue, 25 Jun 2024 22:25:20 -0400 Subject: [PATCH 4/6] Create test.py --- test.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 test.py diff --git a/test.py b/test.py new file mode 100644 index 0000000..6d95fe9 --- /dev/null +++ b/test.py @@ -0,0 +1 @@ +print("Hello world") \ No newline at end of file From 2c430533dfa0817464c599efee2a97409559ebdb Mon Sep 17 00:00:00 2001 From: CalvinTC97 Date: Tue, 25 Jun 2024 22:30:39 -0400 Subject: [PATCH 5/6] Delete test.py --- test.py | 1 - 1 file changed, 1 deletion(-) delete mode 100644 test.py diff --git a/test.py b/test.py deleted file mode 100644 index 6d95fe9..0000000 --- a/test.py +++ /dev/null @@ -1 +0,0 @@ -print("Hello world") \ No newline at end of file From 08fda2d5184b44e0075ba05f61c40e0b278e629c Mon Sep 17 00:00:00 2001 From: CalvinTC97 <62586312+CalvinTC97@users.noreply.github.com> Date: Mon, 12 Aug 2024 18:25:29 -0400 Subject: [PATCH 6/6] Add files via upload --- capacitycheck.py | 418 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 418 insertions(+) create mode 100644 capacitycheck.py diff --git a/capacitycheck.py b/capacitycheck.py new file mode 100644 index 0000000..6c1a6d8 --- /dev/null +++ b/capacitycheck.py @@ -0,0 +1,418 @@ +from constants import PathNeighbourSurvey, PathHungerCount, PathMemberAssetInventory +from Lib import data_loader as dl +import pandas as pd + +df_neighbour_survey = dl.get_data_as_pandas_data_frame(PathNeighbourSurvey) +df_hunger_count = dl.get_data_as_pandas_data_frame(PathHungerCount) +df_member_asset_inventory = dl.get_data_as_pandas_data_frame(PathMemberAssetInventory) + +#MAI calculations - how many days of food are provided per visitor and per month? + +df_slice = df_member_asset_inventory[["id", "MAI_Q004_002", "MAI_Q004_003", "MAI_Q004_004", "MAI_Q004_005", "MAI_Q004_006", "MAI_Q004_007", "MAI_Q004_008", "MAI_Q004_009", "MAI_Q004_010", "MAI_Q004_011", "MAI_Q004_012", "MAI_Q004_013", "MAI_Q004_014", "MAI_Q004_015", "MAI_Q004_016", "MAI_Q004_017", "MAI_Q004_018", "MAI_Q004_019", "MAI_Q004_020", "MAI_Q004_021", "MAI_Q004_022", "MAI_Q004_023", "MAI_Q004_024", "MAI_Q031_001", "MAI_Q031_002", "MAI_Q031_003", "MAI_Q031_004", "MAI_Q031_005", "MAI_Q031_006", "MAI_Q031_007", "MAI_Q031_008", "MAI_Q031_009", "MAI_Q031_010", "MAI_Q031_011", "MAI_Q048", "MAI_Q051", "MAI_Q053_001", "MAI_Q053_002", "MAI_Q053_003", "MAI_Q053_004", "MAI_Q053_005", "MAI_Q053_006", "MAI_Q053_007", "MAI_Q053_008", "MAI_Q008"]] + +num_rows = len(df_slice) +num_supports = 0 +num_between_1_and_3_days = 0 +num_over_3_days = 0 +num_at_least_1_weekend_day = 0 +num_over_500 = 0 +num_over_5_days = 0 +num_atypical_grocery_answer = 0 +Food_bank_IDs = ["A005", "A011", "A020", "A024", "A028", "A029", "A031", "A032", "A037", "A038", "A039", "A041", "A046", "A047", "A050", "A057", "A058", "A060", "A062", "A064", "A065", "A076", "A082"] + +df_slice.insert(21, "Number of wrap-around supports provided", "Empty") +df_slice.insert(22,"Hours of service per month","Empty") +df_slice.insert(23,"Visitors served per hour","Empty") +df_slice.insert(24,"Days of food provided per visitor","Empty") +df_slice.insert(25,"Days of food provided per month","Empty") +df_slice.insert(26, "Type of agency", "Empty") +df_slice.insert(27, "Effective number of staff/volunteers", "Empty") + +x = 0 + +while x < num_rows: + if pd.isna(df_slice.at[x, 'MAI_Q004_002']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_003']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_004']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_005']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_006']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_007']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_008']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_009']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_010']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_011']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_012']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_013']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_014']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_015']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_016']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_017']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_018']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_019']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_020']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_021']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_022']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_023']) == False: + num_supports += 1 + if pd.isna(df_slice.at[x, 'MAI_Q004_024']) == False: + num_supports += 1 + + if num_supports > 0: + df_slice.loc[x, 'Number of wrap-around supports provided'] = num_supports + else: + df_slice.loc[x, 'Number of wrap-around supports provided'] = "NA" + + num_supports = 0 + x += 1 + + + +x = 0 + +while x < num_rows: + if pd.isna(df_slice.at[x,'MAI_Q031_003']) == False: + df_slice.loc[x,'Hours of service per month'] = 64 + num_between_1_and_3_days += 1 + elif pd.isna(df_slice.at[x,'MAI_Q031_004']) == False: + df_slice.loc[x,'Hours of service per month'] = 128 + num_over_3_days += 1 + elif pd.isna(df_slice.at[x,'MAI_Q031_007']) == False: + df_slice.loc[x, 'Hours of service per month'] = 48 + num_at_least_1_weekend_day += 1 + elif pd.isna(df_slice.at[x,'MAI_Q031_008']) == False: + df_slice.loc[x, 'Hours of service per month'] = 16 + elif pd.isna(df_slice.at[x,'MAI_Q031_002']) == False: + df_slice.loc[x, 'Hours of service per month'] = 16 + elif pd.isna(df_slice.at[x,'MAI_Q031_005']) == False: + df_slice.loc[x, 'Hours of service per month'] = 48 + elif pd.isna(df_slice.at[x,'MAI_Q031_006']) == False: + df_slice.loc[x, 'Hours of service per month'] = 8 + elif pd.isna(df_slice.at[x,'MAI_Q031_009']) == False: + df_slice.loc[x, 'Hours of service per month'] = 28 + elif pd.isna(df_slice.at[x,'MAI_Q031_010']) == False: + df_slice.loc[x, 'Hours of service per month'] = 28 + elif pd.isna(df_slice.at[x,'MAI_Q031_011']) == False: + df_slice.loc[x, 'Hours of service per month'] = 28 + else: + df_slice.loc[x, 'Hours of service per month'] = "NA" + + + if df_slice.loc[x, 'MAI_Q051'] == "1-10 visitors": + df_slice.loc[x, 'Visitors served per hour'] = 5 + elif df_slice.loc[x, 'MAI_Q051'] == "11-25 visitors": + df_slice.loc[x, 'Visitors served per hour'] = 18 + elif df_slice.loc[x, 'MAI_Q051'] == "26-50 visitors": + df_slice.loc[x, 'Visitors served per hour'] = 38 + elif df_slice.loc[x, 'MAI_Q051'] == "51-100 visitors": + df_slice.loc[x, 'Visitors served per hour'] = 76 + elif df_slice.loc[x, 'MAI_Q051'] == "101-200 visitors": + df_slice.loc[x, 'Visitors served per hour'] = 151 + elif df_slice.loc[x, 'MAI_Q051'] == "201-500 visitors": + df_slice.loc[x, 'Visitors served per hour'] = 350 + elif df_slice.loc[x, 'MAI_Q051'] == "500+ visitors": + df_slice.loc[x, 'Visitors served per hour'] = 500 + num_over_500 += 1 + else: + df_slice.loc[x, 'Visitors served per hour'] = "NA" + + + if df_slice.loc[x, 'MAI_Q053_001'] == "We provide enough for a meal's worth on average": + df_slice.loc[x, 'Days of food provided per visitor'] = 0.3 + elif df_slice.loc[x, 'MAI_Q053_002'] == "We provide 1-2 days of food on average": + df_slice.loc[x, 'Days of food provided per visitor'] = 1.5 + elif df_slice.loc[x, 'MAI_Q053_003'] == "We provide 3 days of food on average": + df_slice.loc[x, 'Days of food provided per visitor'] = 3 + elif df_slice.loc[x, 'MAI_Q053_004'] == "We are able to provide 4 days of food on average": + df_slice.loc[x, 'Days of food provided per visitor'] = 4 + elif df_slice.loc[x, 'MAI_Q053_005'] == "We are able to provide 5 days of food on average": + df_slice.loc[x, 'Days of food provided per visitor'] = 5 + elif df_slice.loc[x, 'MAI_Q053_006'] == "We are able to provide more than 5 days of food on average": + df_slice.loc[x, 'Days of food provided per visitor'] = 7 + num_over_5_days += 1 + else: + df_slice.loc[x, 'Days of food provided per visitor'] = "NA" + num_atypical_grocery_answer += 1 + + if (isinstance(df_slice.loc[x, 'Hours of service per month'], int) == True) and (isinstance(df_slice.loc[x, 'Visitors served per hour'], int) == True) and (isinstance(df_slice.loc[x, 'Days of food provided per visitor'], int) == True): + df_slice.loc[x, 'Days of food provided per month'] = df_slice.loc[x, 'Hours of service per month']*df_slice.loc[x, 'Visitors served per hour']*df_slice.loc[x, 'Days of food provided per visitor'] + else: + df_slice.loc[x, 'Days of food provided per month'] = "NA" + + if (df_slice.loc[x, 'id'] in Food_bank_IDs) == True: + df_slice.loc[x, 'Type of agency'] = 'Food bank' + else: + df_slice.loc[x, 'Type of agency'] = 'Other food program' + + x += 1 + +food_bank_dataframe_for_food_per_month = pd.DataFrame() +food_bank_dataframe_for_food_per_visitor = pd.DataFrame() +food_program_dataframe_for_food_per_month = pd.DataFrame() +food_program_dataframe_for_food_per_visitor = pd.DataFrame() + +food_bank_dataframe_for_food_per_month.insert(0,"Days of food provided per month","Empty") +food_bank_dataframe_for_food_per_visitor.insert(0,"Days of food provided per visitor","Empty") +food_program_dataframe_for_food_per_month.insert(0, "Days of food provided per month", "Empty") +food_program_dataframe_for_food_per_visitor.insert(0, "Days of food provided per visitor", "Empty") + +x = 0 +x_food_bank = 0 +x_food_program = 0 + +while x < num_rows: + + if isinstance(df_slice.loc[x, 'Days of food provided per month'], int) == True: + if df_slice.loc[x, 'Type of agency'] == 'Food bank': + food_bank_dataframe_for_food_per_month.loc[x_food_bank, 'Days of food provided per month'] = df_slice.loc[x, 'Days of food provided per month'] + x_food_bank += 1 + else: + food_program_dataframe_for_food_per_month.loc[x_food_program, 'Days of food provided per month'] = df_slice.loc[x, 'Days of food provided per month'] + x_food_program += 1 + + x += 1 + +x = 0 +x_food_bank = 0 +x_food_program = 0 + +while x < num_rows: + + if isinstance(df_slice.loc[x, 'Days of food provided per visitor'], int) == True: + if df_slice.loc[x, 'Type of agency'] == 'Food bank': + food_bank_dataframe_for_food_per_visitor.loc[x_food_bank, 'Days of food provided per visitor'] = df_slice.loc[x, 'Days of food provided per visitor'] + x_food_bank += 1 + else: + food_program_dataframe_for_food_per_visitor.loc[x_food_program, 'Days of food provided per visitor'] = df_slice.loc[x, 'Days of food provided per visitor'] + x_food_program += 1 + + x += 1 + +food_bank_food_per_month_lower_quartile = food_bank_dataframe_for_food_per_month.quantile([0.25]) +food_bank_food_per_month_upper_quartile = food_bank_dataframe_for_food_per_month.quantile([0.75]) +food_bank_food_per_visitor_lower_quartile = food_bank_dataframe_for_food_per_visitor.quantile([0.25]) +food_bank_food_per_visitor_upper_quartile = food_bank_dataframe_for_food_per_visitor.quantile([0.75]) + +food_program_food_per_month_lower_quartile = food_program_dataframe_for_food_per_month.quantile([0.25]) +food_program_food_per_month_upper_quartile = food_program_dataframe_for_food_per_month.quantile([0.75]) +food_program_food_per_visitor_lower_quartile = food_program_dataframe_for_food_per_visitor.quantile([0.25]) +food_program_food_per_visitor_upper_quartile = food_program_dataframe_for_food_per_visitor.quantile([0.75]) + +x = 0 + +while x < num_rows: + if pd.isna(df_slice.at[x,'MAI_Q008']) == False: + if df_slice.loc[x, 'MAI_Q008'] == "We have enough staff and/or volunteers to run our food programming effectively": + df_slice.loc[x, 'Effective number of staff/volunteers'] = 1 + else: + df_slice.loc[x, 'Effective number of staff/volunteers'] = 0 + else: + df_slice.loc[x, 'Effective number of staff/volunteers'] = "NA" + + x += 1 + + +print("Food provided per month by a food bank at the lower quartile:") +print(food_bank_food_per_month_lower_quartile) +print("\n") + +print("Food provided per month by a food bank at the upper quartile:") +print(food_bank_food_per_month_upper_quartile) +print("\n") + +print("Food provided per month by a food program at the lower quartile:") +print(food_program_food_per_month_lower_quartile) +print("\n") + +print("Food provided per month by a food program at the upper quartile:") +print(food_program_food_per_month_upper_quartile) +print("\n") + +print("Food provided per visitor by a food bank at the lower quartile:") +print(food_bank_food_per_visitor_lower_quartile) +print("\n") + +print("Food provided per visitor by a food bank at the upper quartile:") +print(food_bank_food_per_visitor_upper_quartile) +print("\n") + +print("Food provided per visitor by a food program at the lower quartile:") +print(food_program_food_per_visitor_lower_quartile) +print("\n") + +print("Food provided per visitor by a food program at the upper quartile:") +print(food_program_food_per_visitor_upper_quartile) +print("\n") + +print("\n") +print("\n") +print("\n") +print("\n") +print("\n") +print("\n") +print("\n") +print("\n") + +df_slice = df_slice.drop(columns=["MAI_Q004_002", "MAI_Q004_003", "MAI_Q004_004", "MAI_Q004_005", "MAI_Q004_006", "MAI_Q004_007", "MAI_Q004_008", "MAI_Q004_009", "MAI_Q004_010", "MAI_Q004_011", "MAI_Q004_012", "MAI_Q004_013", "MAI_Q004_014", "MAI_Q004_015", "MAI_Q004_016", "MAI_Q004_017", "MAI_Q004_018", "MAI_Q004_019", "MAI_Q004_020", "MAI_Q004_021", "MAI_Q004_022", "MAI_Q004_023", "MAI_Q004_024", "MAI_Q031_001", "MAI_Q031_002", "MAI_Q031_003", "MAI_Q031_004", "MAI_Q031_005", "MAI_Q031_006", "MAI_Q031_007", "MAI_Q031_008", "MAI_Q031_009", "MAI_Q031_010", "MAI_Q031_011", "MAI_Q048", "MAI_Q051", "MAI_Q053_001", "MAI_Q053_002", "MAI_Q053_003", "MAI_Q053_004", "MAI_Q053_005", "MAI_Q053_006", "MAI_Q053_007", "MAI_Q053_008", "MAI_Q008"]) + +df_slice.to_csv("MAI calculations.csv", na_rep="NA") + +#Neighbour Survey Calculations - how many visitors work 35hrs a week? and how frequently do visitors visit? + +df_slice = df_neighbour_survey[["q002", "q033c", "q033d", "q033f", "q033g", "q033h", "q041a", "q010", "q011a"]] + +num_rows = len(df_slice) +num_cant_meet_food_needs = 0 +num_some_university_education = 0 +num_employed_35hs = 0 +num_food_bank_visits = 0 +num_food_program_visits = 0 + +df_slice.insert(9,"Can't meet food needs","Empty") +df_slice.insert(10,"At least some post-secondary education","Empty") +df_slice.insert(11,"At least 35hrs of employment a week","Empty") +df_slice.insert(12,"Food bank visits a month","Empty") +df_slice.insert(13,"Food program visits a month","Empty") + +x = 0 + +while x < num_rows: + if df_slice.loc[x, 'q002'] == "No": + df_slice.loc[x, "Can't meet food needs"] = 1 + num_cant_meet_food_needs += 1 + else: + df_slice.loc[x, "Can't meet food needs"] = "NA" + + if df_slice.loc[x, 'q033c'] == 'Some college / university' and df_slice.loc[x, 'q002'] == 'No': + df_slice.loc[x, 'At least some post-secondary education'] = 1 + num_some_university_education += 1 + elif df_slice.loc[x, 'q033d'] == 'Completed college / university' and df_slice.loc[x, 'q002'] == 'No': + df_slice.loc[x, 'At least some post-secondary education'] = 1 + num_some_university_education += 1 + elif df_slice.loc[x, 'q033f'] == 'Some graduate education' and df_slice.loc[x, 'q002'] == 'No': + df_slice.loc[x, 'At least some post-secondary education'] = 1 + num_some_university_education += 1 + elif df_slice.loc[x, 'q033g'] == 'Completed graduate education' and df_slice.loc[x, 'q002'] == 'No': + df_slice.loc[x, 'At least some post-secondary education'] = 1 + num_some_university_education += 1 + elif df_slice.loc[x, 'q033h'] == 'Professional degree' and df_slice.loc[x, 'q002'] == 'No': + df_slice.loc[x, 'At least some post-secondary education'] = 1 + num_some_university_education += 1 + else: + df_slice.loc[x, 'At least some post-secondary education'] = "NA" + + if df_slice.loc[x, 'q041a'] == 'Employed at least 35 hours each week' and df_slice.loc[x, 'q002'] == 'No': + df_slice.loc[x, 'At least 35hrs of employment a week'] = 1 + num_employed_35hs += 1 + else: + df_slice.loc[x, 'At least 35hrs of employment a week'] = "NA" + + if df_slice.loc[x, 'q010'] == 'Every 6 months': + df_slice.loc[x, 'Food bank visits a month'] = 0.17 + elif df_slice.loc[x, 'q010'] == 'Every two months': + df_slice.loc[x, 'Food bank visits a month'] = 0.5 + elif df_slice.loc[x, 'q010'] == 'Less than twice a year': + df_slice.loc[x, 'Food bank visits a month'] = 0 + elif df_slice.loc[x, 'q010'] == 'Once per month': + df_slice.loc[x, 'Food bank visits a month'] = 1 + elif df_slice.loc[x, 'q010'] == 'Two times per month': + df_slice.loc[x, 'Food bank visits a month'] = 2 + else: + df_slice.loc[x, 'Food bank visits a month'] = "NA" + + if df_slice.loc[x, 'q011a'] == 'Daily': + df_slice.loc[x, 'Food program visits a month'] = 30 + elif df_slice.loc[x, 'q011a'] == "don't visit every month": + df_slice.loc[x, 'Food program visits a month'] = 0.33 + elif df_slice.loc[x, 'q011a'] == 'I do not visit other food programs': + df_slice.loc[x, 'Food program visits a month'] = 0 + elif df_slice.loc[x, 'q011a'] == 'Less than every 6 months': + df_slice.loc[x, 'Food program visits a month'] = 0.22 + elif df_slice.loc[x, 'q011a'] == 'More than once a week but less than daily': + df_slice.loc[x, 'Food program visits a month'] = 12 + elif df_slice.loc[x, 'q011a'] == 'Once a week': + df_slice.loc[x, 'Food program visits a month'] = 4 + elif df_slice.loc[x, 'q011a'] == 'Two times a month': + df_slice.loc[x, 'Food program visits a month'] = 2 + else: + df_slice.loc[x, 'Food program visits a month'] = "NA" + + x += 1 + +print("Share of respondents who don't have enough income to meet food needs:") +print(num_cant_meet_food_needs/num_rows) +print("\n") + +print("Share of respondents who don't have enough income who have at least some post-secondary education:") +print(num_some_university_education/num_cant_meet_food_needs) +print("\n") + +print("Share of respondents who don't have enough income who work at least 35hrs a week:") +print(num_employed_35hs/num_cant_meet_food_needs) +print("\n") + +visitor_dataframe_for_food_bank_visits_a_month = pd.DataFrame() +visitor_dataframe_for_food_program_visits_a_month = pd.DataFrame() + +visitor_dataframe_for_food_bank_visits_a_month.insert(0, "Food bank visits a month", "Empty") +visitor_dataframe_for_food_program_visits_a_month.insert(0, "Food program visits a month", "Empty") + +x = 0 +x_food_bank = 0 +x_food_program = 0 + +while x < num_rows: + + if isinstance(df_slice.loc[x, 'Food bank visits a month'], int) == True: + visitor_dataframe_for_food_bank_visits_a_month.loc[x_food_bank, 'Food bank visits a month'] = df_slice.loc[x, 'Food bank visits a month'] + x_food_bank += 1 + + if isinstance(df_slice.loc[x, 'Food program visits a month'], int) == True: + visitor_dataframe_for_food_program_visits_a_month.loc[x_food_program, 'Food program visits a month'] = df_slice.loc[x, 'Food program visits a month'] + x_food_program += 1 + + x += 1 + +visitor_food_bank_visits_a_month_lower_quartile = visitor_dataframe_for_food_bank_visits_a_month.quantile([0.25]) +visitor_food_bank_visits_a_month_upper_quartile = visitor_dataframe_for_food_bank_visits_a_month.quantile([0.75]) +visitor_food_program_visits_a_month_lower_quartile = visitor_dataframe_for_food_program_visits_a_month.quantile([0.25]) +visitor_food_program_visits_a_month_upper_quartile = visitor_dataframe_for_food_program_visits_a_month.quantile([0.75]) + +print("Number of visits per month to a food bank at the lower quartile:") +print(visitor_food_bank_visits_a_month_lower_quartile) +print("\n") + +print("Number of visits per month to a food bank at the upper quartile:") +print(visitor_food_bank_visits_a_month_upper_quartile) +print("\n") + +print("Number of visits per month to a food program at the lower quartile:") +print(visitor_food_program_visits_a_month_lower_quartile) +print("\n") + +print("Number of visits per month to a food program at the upper quartile:") +print(visitor_food_program_visits_a_month_upper_quartile) +print("\n") + +df_slice.to_csv("Neighbour Survey calculations.csv", na_rep="NA") +