Skip to content
0

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 pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df_food = pd.read_csv('nutrition.csv')
df_food
df_food.info()
df_food.isnull().sum()
df_food.duplicated().sum()

๐Ÿ’ช 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.
# Which fruit has the highest vitamin C content? What are some other sources of vitamin C?

categories=df_food['Category'].value_counts()

fruits = df_food[(df_food['Category']=='Fruits and Fruit Juices')]

high_vitC_fruit_value = fruits.sort_values(by='Vitamin C',ascending=False).index[0]

high_vitC_fruit=df_food.loc[high_vitC_fruit_value,'Item']

high_vitC_fruit                                     
# What are some other sources of vitamin C?

sorted_fruits = fruits.sort_values(by='Vitamin C', ascending=False)
vitC_sources = sorted_fruits[['Item', 'Vitamin C']]
vitC_sources.head(10)
# Describe the relationship between the calories and water content of a food item.

df_food['Calories'] = df_food['Calories'].astype(str).str.strip('kcal').astype(float)
df_food['Water'] = df_food['Water'].astype(str).str.strip('g').astype(float)
water_and_calories = df_food[['Category', 'Item', 'Calories', 'Water']]
water_and_calories.head()

1 hidden cell

import plotly.express as px

correlation = water_and_calories['Calories'].corr(water_and_calories['Water'])
print(f"Correlation between Calories and Water content: {correlation:.2f}")


fig = px.scatter(
    water_and_calories,
    x='Calories',
    y='Water',
    color='Category',
    title='Scatter Plot of Calories vs. Water Content',
    labels={'Calories': 'Calories (kcal)', 'Water': 'Water Content (g)'},
    hover_data=['Item', 'Category'],
)
fig.show()

The correlation is very high and it seems the higher water content is the less the calories. But at low water content there are products with higher calories. Breakfast Cereals, Nut and Seed Products, Spices and Herbs categories have more calories with low water content. It is not suprising.

#What are the possible drawbacks of a zero-carb diet? What could be the drawbacks of a very high-protein diet?

df_food['Carbohydrate'] = df_food['Carbohydrate'].astype(str).str.strip('g').astype(float)
df_food['Protein'] = df_food['Protein'].astype(str).str.strip('g').astype(float)
df_food['Total fat']=df_food['Total fat'].astype(str).str.strip('g').astype(float)
df_food['Cholesterol']=df_food['Cholesterol'].astype(str).str.strip('mg').astype(float)
df_food['Fiber']=df_food['Fiber'].astype(str).str.strip('g').astype(float)
df_food['Vitamin C']=df_food['Vitamin C'].astype(str).str.strip('mg').astype(float)
df_food['Alcohol']=df_food['Alcohol'].astype(str).str.strip('g').astype(float)

zero_carb = df_food[df_food['Carbohydrate'] == 0]
zero_carb

2 hidden cells
โ€Œ
โ€Œ
โ€Œ