Skip to content
Battle of the Nutrients: Unveiling the Truth in Our Nutrition Daily Challenge (copy)
  • AI Chat
  • Code
  • Report
  • What is good food?

    📖 Background

    You and your friend have gotten into a debate about nutrition. Your friend follows a high-protein diet and does not eat any carbohydrates (no grains, no fruits). You claim that a balanced diet should contain all nutrients but should be low in calories. Both of you quickly realize that most of what you know about nutrition comes from mainstream and social media.

    Being the data scientist that you are, you offer to look at the data yourself to answer a few key questions.

    💾 The data

    You source nutrition data from USDA's FoodData Central website. This data contains the calorie content of 7,793 common foods, as well as their nutritional composition. Each row represents one food item, and nutritional values are based on a 100g serving. Here is a description of the columns:

    • FDC_ID: A unique identifier for each food item in the database.
    • Item: The name or description of the food product.
    • Category: The category or classification of the food item, such as "Baked Products" or "Vegetables and Vegetable Products".
    • Calories: The energy content of the food, presented in kilocalories (kcal).
    • Protein: The protein content of the food, measured in grams.
    • Carbohydrate: The carbohydrate content of the food, measured in grams.
    • Total fat: The total fat content of the food, measured in grams.
    • Cholesterol: The cholesterol content of the food, measured in milligrams.
    • Fiber: The dietary fiber content of the food, measured in grams.
    • Water: The water content of the food, measured in grams.
    • Alcohol: The alcohol content of the food (if any), measured in grams.
    • Vitamin C: The Vitamin C content of the food, measured in milligrams.
    # import the necessary libraries
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    import seaborn as sns
    from matplotlib import rcParams
    from scipy import stats
    import statsmodels.api as sm
    from sklearn.linear_model import LinearRegression
    import pandas as pd
    
    df_food = pd.read_csv('nutrition.csv')
    df_food

    💪 Competition challenge

    Create a report that covers the following:

    1. Which fruit has the highest vitamin C content? What are some other sources of vitamin C?
    2. Describe the relationship between the calories and water content of a food item.
    3. What are the possible drawbacks of a zero-carb diet? What could be the drawbacks of a very high-protein diet?
    4. According to the Cleveland Clinic website, a gram of fat has around 9 kilocalories, and a gram of protein and a gram of carbohydrate contain 4 kilocalories each. Fit a linear model to test whether these estimates agree with the data.
    5. Analyze the errors of your linear model to see what could be the hidden sources of calories in food.

    Executive Summary: Guiding Insights and Recommendations for Nutritional Wellness

    In appreciation of engaged readers, this report unveils a collective exploration of nutrition challenges, driven by your invaluable participation. As we venture into the intricate world of nutrition, your involvement stands as a beacon, fostering a shared understanding that propels my journey.

    Key Recommendations:

    Maximizing Vitamin C Intake: Recommending the incorporation of diverse vitamin C-rich foods, such as bell peppers and strawberries, to optimize nutritional intake.

    Caloric and Hydration Dynamics: Our exploration highlights a compelling relationship between calories and water content. Encouraging further research into the implications of this correlation for making informed dietary choices.

    Balancing Dietary Patterns: Delving into the drawbacks of zero-carb and high-protein diets, we advocate for a balanced approach. Acknowledging the benefits of moderation and variety in sustaining a healthy lifestyle.

    Rethinking Caloric Estimates: Our linear model challenges conventional caloric estimates, emphasizing the importance of data-driven scrutiny. Suggesting a continuous evaluation of nutritional information to refine our understanding of energy content.

    Which fruit has the highest vitamin C content?

    #Displays concise information about the DataFrame df_food
    df_food.info()
    #Evaluates and reports the count of missing values (NaN) in each column of the DataFrame df_food.
    df_food.isna().sum()
    #Duplicate the dataset 
    df_food_copy = df_food.copy()
    #Cleaning the string characters from the column of Vitamin C
    df_food_copy["Vitamin C"] = df_food_copy["Vitamin C"].str.replace("mg" , " ")
    #Print the first rows of cleaned Vitamin column
    df_food_copy["Vitamin C"].head()
    #Convert the 'Vitamin C' column in the DataFrame df_food_copy to float data type
    df_food_copy["Vitamin C"] = df_food_copy["Vitamin C"].astype(float) 
    # Group the DataFrame df_food_copy by the 'Item' column and calculate the sum of 'Vitamin C' for each group
    df_Total_C = df_food_copy.groupby(["Item","Category"])["Vitamin C"].sum().reset_index()
    #Display the first 20 rows of the resulting DataFrame df_Total_C
    df_Total_C.head(20)
    # Filter rows where the category is "Fruits and Fruit Juices"
    filtered_df = df_Total_C[df_Total_C['Category'] == 'Fruits and Fruit Juices']
    #Sort the DataFrame df_Total_C by the 'Vitamin C' column in descending order
    high_C = filtered_df.sort_values("Vitamin C", ascending=False)
    # Access the value in the 'Vitamin C' column of the first row using iloc
    # This corresponds to the highest Vitamin C value in the DataFrame
    result = high_C[['Category', 'Item','Vitamin C']] 
    result.iloc[0]

    What are some other sources of vitamin C?

    # Create an empty list named 'source'
    source = []
    
    # Filter rows in the DataFrame df_Total_C where 'Vitamin C' is not equal to 0
    source = df_Total_C.loc[df_Total_C['Vitamin C'] != 0]
    
    # Sort the filtered DataFrame 'source' by the 'Vitamin C' column in descending order
    high_C = source.sort_values("Vitamin C", ascending=False)
    
    # Print the resulting DataFrame 'high_C'
    print(high_C)