I collected the nutrition data sets from Kaggle and posted them respectively on githubs, to ensure that its easier to extract the datasets for cleaning utilizing pandas.
import pandas as pd
urls = [
"<https://raw.githubusercontent.com/Ethan004-Code/Food_nutrition_dataset/main/FOOD-DATA-GROUP1.csv>",
"<https://raw.githubusercontent.com/Ethan004-Code/Food_nutrition_dataset/main/FOOD-DATA-GROUP2.csv>",
"<https://raw.githubusercontent.com/Ethan004-Code/Food_nutrition_dataset/main/FOOD-DATA-GROUP3.csv>",
"<https://raw.githubusercontent.com/Ethan004-Code/Food_nutrition_dataset/main/FOOD-DATA-GROUP4.csv>",
"<https://raw.githubusercontent.com/Ethan004-Code/Food_nutrition_dataset/main/FOOD-DATA-GROUP5.csv>"
]
dfs = [pd.read_csv(url) for url in urls]
combined_df = pd.concat(dfs, ignore_index=True)
print(combined_df.shape)
combined_df.head()
for i, df in enumerate(dfs, 1):
print(f"--- Dataset {i} ---")
display(df)
print("\n")
I standardized column names by converting them to lowercase, removing extra spaces, and cleaning punctuation, this ensures consistency for the codes function.
I removed empty rows filled with missing numerical data or columns with 0 or unknown strings with āUnknownā. This ensured that no non numerical values would break the codes functionality.
for name, df in csv_dfs.items():
df.columns = (
df.columns.str.lower()
.str.strip()
.str.replace(" ", "_")
.str.replace(r'[^\w\s]', '', regex=True)
)
After merging and cleaning all the nutrition dataset, I exported the cleaned datasets to a readable textfile to review the dataset and then exported it to a new excel file for download.
This is so that i could test out the main python functions and coding environment more proficiently, this textfile will better in terms of readablity for debugging and will later be used in both theĀ backend APIĀ andĀ frontend display.
with open('cleaned_food_data.txt', 'w') as f:
f.write(combined_df.to_string(index=False))
from google.colab import files
files.download('cleaned_food_data.txt')
This is to ensure the system is cloud connected, I loaded my cleaned nutrition dataset to github and imported into google collabs which allows me to test run the cleaned dataset. Moreover this also allows the backend/ frontend to run my python functions on updates without storing local files onto google collabs.
import pandas as pd
from io import StringIO
import requests
from rapidfuzz import process
url = "<https://raw.githubusercontent.com/Ethan004-Code/Food_nutrition_dataset/main/cleaned_food_data.txt>"
response = requests.get(url)
text_data = response.text
data = StringIO(text_data)
df = pd.read_fwf(data)
A simple dataset to find low calorie items, foods with fewer than 40 calories per 100 gram would be displayed in the output. This used in frontend filters for health-conscious features.
low_calorie_foods = df[df['Value'] < 40]
print(low_calorie_foods)
I developed a custom scoring function , this function helps evaluates a food item based on its fat, sugar, sodium, fiber, protein and cholesterol levels. This range can be used in health advice systems for meal quality better ratings and advice.
def compute_health_score(row):
score = 0
if row['Fat'] < 5:
score += 2
if row['Saturated Fats'] < 1.5:
score += 2
if row['Sugars'] < 5:
score += 2
if row['Dietary Fiber'] > 3:
score += 2
if 5 <= row['Protein'] <= 15:
score += 1
if row['Sodium'] < 140:
score += 2
if row['Cholesterol'] < 30:
score += 2
return score