Home > Enterprise >  what would be an efficient way to rank the top 3 co2 datas?
what would be an efficient way to rank the top 3 co2 datas?

Time:02-22

how can I make use of Numpy to rank the top 3 co2 data with the year? the image below shows what I have got so far, I am able to zip them together but I am not quite sure on how to sort and print the top 3 without messing with the indexes.

https://i.stack.imgur.com/RIsQq.png

CodePudding user response:

With python's standard library: heapq.nlargest

from heapq import nlargest

c = [2013, 2014, 2015, 2016, 2017, 2018]
x = [10., 9.57, 9.27, 9.32, 9.72, 9.65]

for v,y in nlargest(3, zip(x,c)):
    print('Year: {};  CO2: {}'.format(y,v))
# Year: 2013;  CO2: 10.0
# Year: 2017;  CO2: 9.72
# Year: 2018;  CO2: 9.65

With numpy: numpy.argpartition

import numpy as np

cx = np.array([[2013, 2014, 2015, 2016, 2017, 2018],
               [10., 9.57, 9.27, 9.32, 9.72, 9.65]])

idx_top3 = np.argpartition(cx, (-3,-2,-1))

cx_top3 = cx[:, idx_top3[1,-3:]]

print(cx_top3)
# [[2018.   2017.   2013.  ]
#  [   9.65    9.72   10.  ]]

See also: pandas.DataFrame.nlargest

  • Related