I'm fairly new to Python, so I apologize if this question seems a little naive. I'm working on a final project for my comp sci class that involves visualizing some data, but I ran into some difficulty with it.
Basically, I'm using a CSV file (which you can find here) that contains reported emissions data from every country over a 29-year period -- but I only want to visualize data from the top five emitting countries in each given year. My code right now looks like this:
counter = 0
for i in range(30):
global_emissions = {}
for country in dataset:
'''dataset is a list of country-specific dictionaries; a key value for every year (e.g. '1991') corresponds to a float representing CO2 emissions in kt.'''
key = str(1990 + counter)
emissions = country[key]
global_emissions[country['COUNTRY']] = emissions
sorted_emissions = {}
for i in sorted(global_emissions.values()): # this loop is just here to match keys to sorted values
for k in global_emissions.keys():
if global_emissions[k] == i:
sorted_emissions[k] = global_emissions[k]
counter += 1
The sorted_emissions
list for any given year then comes out as a bunch of key-value pairs in ascending order of value. At the very end are the highest-polluting countries. In the year 1990, for example, it looks like this:
...'Germany': 955310.0, 'Japan': 1090530.0, 'Russian Federation': 2163530.0, 'China': 2173360.0, 'United States': 4844520.0}
For the dictionaries that get produced for all 29 years, I want to isolate these last five elements -- but since the keys of the most-polluting nations will change from year to year, I can't just access them by their key.
I tried indexing to them with numerical slicing (i.e. trying something like sorted_emissions[-1]
), but this doesn't work with dictionaries. Is there any workaround for this, or maybe just a better way to do it? Any suggestions at all would be appreciated. Thanks so much!