I need help figuring out why my code is returning incorrect values. I am using the max function to try and retreive the highest values based off of the following criterion: locality or localities that distributed the most pounds of food, the most individuals served, and the most households served for the date April 2019.
When using the max function, it is returning 999 for both individuals and households served. This is incorrect because when comparing the output to the actual data from xml, you can see that there are higher values.
Here is what I have tried:
import requests
from lxml import objectify
URL = 'https://data.virginia.gov/api/views/xvir-sctz/rows.xml?accessType=DOWNLOAD'
response = requests.get(URL).content
import requests
from lxml import objectify
#parse xml file
root = objectify.fromstring(response)
#print data
print(response)
#create emtpy lists to store wanted data
localities = []
pounds_of_food_distributed = []
individuals_served = []
households_served = []
#assign data values to varibles
localities = root.xpath('//response/row/row/locality/text()')
pounds_of_food_distributed = root.xpath('//response/row/row/pounds_of_food_distributed/text()')
individuals_served = root.xpath('//response/row/row/individuals_served/text()')
households_served = root.xpath('//response/row/row/households_served/text()')
#for loop to iterate through data set
for row in root.iterchildren():
if row.get("month") == ("April") and row.get("Year") == "2019":
localities.append(row.get("Locality"))
pounds_of_food_distributed.append(row.get("Pounds"))
individuals_served.append(row.get("Individuals"))
households_served.append(row.get("Households"))
#Indexing to retrieve max value of each requested variable
max_pounds_index = pounds_of_food_distributed.index(max(pounds_of_food_distributed))
max_individuals_index = individuals_served.index(max(individuals_served))
max_households_index = households_served.index(max(households_served))
#print values
print("Locality with greatest distribution of food in pounds: ", localities[max_pounds_index])
print("Greatest number of individuals served: ", individuals_served[max_individuals_index])
print("Greatest number of households served: ", households_served[max_households_index])
Locality with greatest distribution of food in pounds: Colonial Heights City
Greatest number of individuals served: 999.0
Greatest number of households served: 999.0
As you can see, the return values do not actually reflect the xml data.
First of all check line 27 pounds_distributed
is not defined, when i was trying to reproduce your problem i bumped into this, i switched between pounds_distributed
and pounds_of_food_distributed
.
Returning to your problem, the indexing is not working beacuse all of the list values are strings and the max function is working lexicographic you can see the issue opened here and the solution: Max function ignoring double digits