The dataset used in this project is a CSV file of the Global Food Prices Database by WFP - World Food Programme.
In [6]:
# Open the file and read its content.
raw_data = open('WFPVAM_FoodPrices_24-01-2017.csv', 'r').read()
In [7]:
# Split the raw_data on every newline.
raw_data = raw_data.split('\n')
# Take of the headers
raw_data_no_header = raw_data[1:]
In [8]:
# Make a list of lists of the raw_data_no_header
staples_data = []
for food_info in raw_data_no_header:
info = food_info.split(',')
staples_data.append(info)
In [9]:
def load_data_by_country(country):
"""Fetch data based on a specific country"""
country_data = []
for info in staples_data:
if country in info:
country_data.append(info)
return(country_data)
In [10]:
# Fetch all data on Ghana
ghana_food_stapes = load_data_by_country(country="Ghana")
In [11]:
# Regions where there are market activities
regions = []
for info in ghana_food_stapes:
current_region = info[3]
if current_region not in regions:
regions.append(current_region)
In [13]:
for region in sorted(regions):
print("{} Region".format(region))
In [16]:
# Market locations across the country
markets = []
for info in ghana_food_stapes:
current_market = info[5]
if current_market not in markets:
markets.append(current_market)
In [20]:
# Markets per Regions
markets_in_regions = {}
for region in regions:
current_market = []
for market in markets:
for info in ghana_food_stapes:
if region == info[3] and market == info[5]:
if market not in current_market:
current_market.append(market)
markets_in_regions.update({region: current_market})
In [24]:
# Loop through the markets_in_regions dictionary
for region, markets in sorted(markets_in_regions.items()):
print("\n{} REGION".format(region.upper()))
if len(markets) > 1:
# If market more than 1 make location plural
print(" {} Market Locations:".format(len(markets)))
elif len(markets) == 1:
# If only 1 market, make location singular
print(" {} Market Location:".format(len(markets)))
for market in sorted(markets):
print("\t{}".format(market))
In [26]:
staples = []
for info in ghana_food_stapes:
current_staple = info[7]
if current_staple not in staples:
staples.append(current_staple)
for staple in sorted(staples):
print(staple)
In [ ]: