We can use for
loops and if
statements to filter through data.
rows = [
{"name": "Rachel", "age": 34},
{"name": "Monica", "age": 34},
{"name": "Phoebe", "age": 37}
]
# filter to age < 37
millenials = []
for row in rows:
if row['age'] < 37:
millenials.append(row)
print(millenials)
# filter whitelist names
cool_people = []
whitelist = ['Rachel', 'Phoebe']
for row in rows:
if row['name'] in whitelist:
cool_people.append(row)
print(cool_people)
# filter blacklist names
cool_people = []
blacklist = ['Monica']
for row in rows:
if row['name'] not in blacklist:
cool_people.append(row)
print(cool_people)
If you use Python's Pandas library for data manipulation and analysis instead, the code would look like this: https://gist.github.com/AlJohri/59c9762845519f999eb28fe45276f4c1
- Read
output/vegetables.csv
into a variable calledvegetables
. - Loop through
vegetables
and filter down to only green vegtables using a whitelist. - Print veggies to the terminal
- Write the veggies to a json file called
output/greenveggies.json
Bonus:
Output another csv called output/green_vegetables.csv
.