|
| 1 | +# +----------------------------------------------------------------------------+ |
| 2 | +# | CARDUI TECH v1.0.0 |
| 3 | +# +----------------------------------------------------------------------------+ |
| 4 | +# | Copyright (c) 2026 - 2026, CARDUITECH.COM (www.carduitech.com) |
| 5 | +# | Vanessa Reteguín <vanessa@reteguin.com> |
| 6 | +# | Released under the MIT license |
| 7 | +# | www.carduitech.com/license/ |
| 8 | +# +----------------------------------------------------------------------------+ |
| 9 | +# | Author.......: Vanessa Reteguín <vanessa@reteguin.com> |
| 10 | +# | First release: March 9, 2026 |
| 11 | +# | Last update..: March 9, 2026 |
| 12 | +# | WhatIs.......: Bacteria Analysis with Pandas - Main |
| 13 | +# +----------------------------------------------------------------------------++ |
| 14 | +# ------------------------- Instructions ----------------------- |
| 15 | + |
| 16 | +# ------------ Resources / Documentation involved ------------- |
| 17 | +# documentation for Kaggle API *within* python?: https://stackoverflow.com/questions/55934733/documentation-for-kaggle-api-within-python |
| 18 | +# Kaggle CLI Documentation: https://github.com/Kaggle/kaggle-cli/blob/main/docs/README.md#authentication |
| 19 | +# Matplotlib List of named colors: https://matplotlib.org/stable/gallery/color/named_colors.html |
| 20 | + |
| 21 | +# Original dataset (Bacteria Dataset): https://www.kaggle.com/datasets/kanchana1990/bacteria-dataset |
| 22 | + |
| 23 | +# Inspiration from: |
| 24 | +# Bacteria Analysis: https://www.kaggle.com/code/osamaalfa/bacteria-analysis |
| 25 | + |
| 26 | +# ------------------------- Libraries ------------------------- |
| 27 | +from kaggle.api.kaggle_api_extended import KaggleApi |
| 28 | +import os # os.path.exists(path) |
| 29 | +import pandas as pd |
| 30 | +import matplotlib.pyplot as plt |
| 31 | + |
| 32 | +# -------------------------- Imports -------------------------- |
| 33 | + |
| 34 | + |
| 35 | +# ------------------------- Functions ------------------------- |
| 36 | + |
| 37 | + |
| 38 | +# ------------------------- Variables ------------------------- |
| 39 | +datasetFolder = 'BacteriaDataset' |
| 40 | +datasetId = 'kanchana1990/bacteria-dataset' |
| 41 | +global datasetPath |
| 42 | + |
| 43 | +# --------------------------- Code ---------------------------- |
| 44 | +try: |
| 45 | + datasetPath = f'{datasetFolder}/{os.listdir(datasetFolder)[0]}' |
| 46 | +except FileNotFoundError or IndexError: |
| 47 | + print(f'File not found. Downloading dataset at {datasetFolder}') |
| 48 | + api = KaggleApi() |
| 49 | + api.authenticate() |
| 50 | + api.dataset_download_files(datasetId, path=datasetFolder, unzip=True) |
| 51 | + print("File successfully downloaded") |
| 52 | + |
| 53 | +# Transform csv into a pandas dataframe |
| 54 | +BacteriaData = pd.read_csv(datasetPath) |
| 55 | + |
| 56 | +# Get data as a dictionary |
| 57 | +bacteria_dic = BacteriaData.to_dict() |
| 58 | +# print(bacteria_dic) |
| 59 | + |
| 60 | +# Example of data (4 rows) |
| 61 | +# print(BacteriaData.head()) |
| 62 | + |
| 63 | +# A summary of our data |
| 64 | +# print(BacteriaData.describe()) |
| 65 | + |
| 66 | +# Get Data in Columns |
| 67 | +# print(BacteriaData["Where Found"]) |
| 68 | +# print(BacteriaData.Family) |
| 69 | + |
| 70 | +# Get Data in Row |
| 71 | +# print(BacteriaData[BacteriaData.Family == "Bacillaceae"]) |
| 72 | + |
| 73 | + |
| 74 | +# Create a dataframe from scratch |
| 75 | +data_dict = { |
| 76 | + "students": ["María", "Jaime", "Luis"], |
| 77 | + "grades": [89, 91, 72] |
| 78 | +} |
| 79 | +data = pd.DataFrame(data_dict) |
| 80 | +# print(data) |
| 81 | + |
| 82 | +# Transform data into a new csv file |
| 83 | +data.to_csv("test_grades.csv") |
| 84 | + |
| 85 | +# Graph |
| 86 | +harmful_counts = BacteriaData["Harmful to Humans"].value_counts() |
| 87 | +# print(harmful_counts) |
| 88 | + |
| 89 | +# Creating the Pie Chart |
| 90 | +# plt.figure(figsize=(6, 6)) |
| 91 | +# plt.pie(harmful_counts, labels=harmful_counts.index, autopct='%1.1f%%', colors=['mediumturquoise', 'lightcoral']) |
| 92 | +# plt.title("Percentage of Harmful vs Non-Harmful Bacteria") |
| 93 | +# plt.show() |
| 94 | + |
| 95 | +# Calculating the 10 Most Common Bacterial Families |
| 96 | +""" |
| 97 | +top_families = BacteriaData["Family"].value_counts().nlargest(10) |
| 98 | +
|
| 99 | +plt.figure(figsize=(14, 5)) |
| 100 | +plt.barh(top_families.index, top_families.values, color='mediumslateblue') |
| 101 | +plt.title('10 Most Common Bacterial Families') |
| 102 | +plt.xlabel('Number of Bacteria') |
| 103 | +plt.ylabel('Family') |
| 104 | +plt.show() |
| 105 | +""" |
| 106 | + |
| 107 | +# Calculating the 5 most common places bacteria was found |
| 108 | +""" |
| 109 | +top_places = BacteriaData["Where Found"].value_counts().nlargest(5) |
| 110 | +
|
| 111 | +plt.figure(figsize=(15, 5)) |
| 112 | +plt.barh(top_places.index, top_places.values, color='sandybrown') |
| 113 | +plt.title('5 Most Common Places Bacteria Was Found') |
| 114 | +plt.xlabel('Number of Bacteria') |
| 115 | +plt.ylabel('Place') |
| 116 | +plt.show() |
| 117 | +""" |
0 commit comments