-
Notifications
You must be signed in to change notification settings - Fork 47
/
get_nutrition_data.py
151 lines (145 loc) · 6.16 KB
/
get_nutrition_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import requests
import pandas as pd
def get_nutrition(food_name):
nutrition_data = pd.DataFrame(columns=['name', 'protein', 'calcium', 'fat', 'carbohydrates', 'vitamins'])
for name in food_name:
url = "https://api.nal.usda.gov/fdc/v1/foods/search?api_key=d4D6dSOc81pTAOY2gsNZ0YhjkMlhStLJRoII5SJu&query=" + name
response = requests.get(url)
data = response.json()
flatten_json = pd.json_normalize(data["foods"])
first_food = flatten_json.iloc[0]
first_food_nutrition_list = first_food.foodNutrients
for item in first_food_nutrition_list:
if item['nutrientNumber'] == "203":
protein = item['value']
continue
if item['nutrientNumber'] == "301":
calcium = item['value']
continue
if item['nutrientNumber'] == "204":
fat = item['value']
continue
if item['nutrientNumber'] == "205":
carbs = item['value']
continue
if item['nutrientNumber'] == "318":
vitamin_a = item['value']
continue
if item['nutrientNumber'] == "401":
vitamin_c = item['value']
continue
vitamins = float(vitamin_a) + float(vitamin_c)
print(name)
nutrition_data = nutrition_data.append({
'name': name,
'protein': protein,
'calcium': calcium / 1000,
'fat': fat,
'carbohydrates': carbs,
'vitamins': vitamins / 1000
}, ignore_index=True)
return nutrition_data
nutrition101 = get_nutrition(['apple pie',
'baby back ribs',
'baklava',
'beef carpaccio',
'beef tartare',
'beet salad',
'beignets',
'bibimbap',
'bread pudding',
'breakfast burrito',
'bruschetta',
'caesar salad',
'cannoli',
'caprese salad',
'carrot cake',
'ceviche',
'cheese plate',
'cheesecake',
'chicken curry',
'chicken quesadilla',
'chicken wings',
'chocolate cake',
'chocolate mousse',
'churros',
'clam chowder',
'club sandwich',
'crab cakes',
'creme brulee',
'croque madame',
'cup cakes',
'deviled eggs',
'donuts',
'dumplings',
'edamame',
'eggs benedict',
'escargots',
'falafel',
'filet mignon',
'fish and_chips',
'foie gras',
'french fries',
'french onion soup',
'french toast',
'fried calamari',
'fried rice',
'frozen yogurt',
'garlic bread',
'gnocchi',
'greek salad',
'grilled cheese sandwich',
'grilled salmon',
'guacamole',
'gyoza',
'hamburger',
'hot and sour soup',
'hot dog',
'huevos rancheros',
'hummus',
'ice cream',
'lasagna',
'lobster bisque',
'lobster roll sandwich',
'macaroni and cheese',
'macarons',
'miso soup',
'mussels',
'nachos',
'omelette',
'onion rings',
'oysters',
'pad thai',
'paella',
'pancakes',
'panna cotta',
'peking duck',
'pho',
'pizza',
'pork chop',
'poutine',
'prime rib',
'pulled pork sandwich',
'ramen',
'ravioli',
'red velvet cake',
'risotto',
'samosa',
'sashimi',
'scallops',
'seaweed salad',
'shrimp and grits',
'spaghetti bolognese',
'spaghetti carbonara',
'spring rolls',
'steak',
'strawberry shortcake',
'sushi',
'tacos',
'octopus balls',
'tiramisu',
'tuna tartare',
'waffles']
)
nutrition101 = nutrition101.reset_index(drop=True)
nutrition101.to_csv("nutrition101.csv")